Working with arrays in PHP is very common for web developers and often used to hold data which is needed later on, for example lists, configurations, etc. PHP provides a great set of array functions which allow you to do a lot of things with your arrays.
Often I find myself wanting to produce some kind of content based on an array, but I don’t need the same action caried out on each element of the array, depending of being the first, any element or maybe the last.
GET params example
You wish to create a URL with some GET params, which you have in an array with key => value association. The ending GET string should be ?key1=val1&key2=val2 etc.
The trick here is that you want to start the result with a ?, not a &. Each key/value pair should be marked up with a = between them. Furthermore you want to apply URL encoding to the value part, to make it work properly.
All this could be done with a complex structure of for/foreach and if/else statements, building your params string. But honestly, while this is a specific example, it is a typical task to perform such actions on array elements, just with varying params for the string output. Therefore a helper class might be really useful.
ArrayToString utility class for easy conversions
Check out this object, which provides the basic functions you need to build any type of string based on an array.
/** * Array to String Utility * * This class can combine elements of an array to a string with more complex options than PHP's implode() function. * * @author Jakob Jensen* @version 1.0 */ class ArrayToString { /** * Process string even if array is empty. * If true and the array is empty, the string will only contain the beforeFirst and afterLast content. * If false and the array is empty, the string will be empty (default). * * @since 1.0 * @var string */ var $processEmptyArray = false; /** * Value to be added before first element * * @since 1.0 * @var string */ var $beforeFirst; /** * Value to be added before each element - except the first. * * @since 1.0 * @var string */ var $beforeEachExceptFirst; /** * Value to be added before each element * * @since 1.0 * @var string */ var $beforeEach; /** * Handler for the key. * Should be array or string like for call_user_func() which will execute with the key of the given element. * * @since 1.0 * @var string */ var $keyHandler; /** * Value to be added between the key and value for each element. * * @since 1.0 * @var string */ var $betweenKeyValue; /** * Handler for the value. * Should be array or string like for call_user_func() which will execute with the value of the given element. * * @since 1.0 * @var string */ var $valueHandler; /** * Value to be added after each element. * * @since 1.0 * @var string */ var $afterEach; /** * Value to be added after each element - except the last. * * @since 1.0 * @var string */ var $afterEachExceptLast; /** * Value to be added after the last element. * * @since 1.0 * @var string */ var $afterLast; /** * Get the processed string for the given array * * @since 1.0 * @param array $array to be converted to string * @return string converted array to string */ public function getString($array) { $string = ''; $count = count($array); $i = 0; foreach ($array as $key => $value) { if ($i==0) $string .= $this->add('beforeFirst'); else if ($i>0) $string .= $this->add('beforeEachExceptFirst'); $string .= $this->add('beforeEach'); $string .= $this->handler('keyHandler', $key); $string .= $this->add('betweenKeyValue'); $string .= $this->handler('valueHandler', $value); $string .= $this->add('afterEach'); if ($i<$count-1) $string .= $this->add('afterEachExceptLast'); else if ($i==$count-1) $string .= $this->add('afterLast'); $i++; } return $string; } /** * Add event value * Checks if any value has been given for the event and returns it. * * @since 1.0 * @param string $event name * @return string event value or empty. */ private function add($event) { if (!is_null($this->$event)) return $this->$event; else return ''; } /** * Process event handler * Checks if any value has been given for the event handler and executes it. * * @since 1.0 * @param string $event name * @param mixed $value with the event value (the key or value from the array being processed) * @return string processed event value */ private function handler($event, $value) { if (!is_null($this->$event)) return call_user_func($this->$event, $value); else return $value; } }
Example usage of ArrayToString utility
$ats = new ArrayToString(); $ats->beforeFirst = '?'; $ats->valueHandler = 'urlencode'; $ats->betweenKeyValue = '='; $ats->afterEachExceptLast = '&'; $url .= $ats->getString($params);
This is the code needed for the GET param builder like mentioned above. Very simple.
No comments yet (leave a comment)