
Coming from PHP, I’m well versed in using the handy explode function to force a string into an array, using a specified delimiter character to chop up the string into the little bits that are to be stored in the array. Happily for me, plain old vanilla JavaScript also has this ability built into the language, choosing to call its version of this functionality “split”. From the official definition: The split() method is used to split a string into an array of substrings, returning the new array containing the substrings. It accepts two parameters, namely the separator and the limit, both of which are actually optional by the way. The separator parameter is where you specify the delimiter character (if you omit this, the entire string will be stuffed into a single element array ). The optional limit parameter if set will control the number of splits. A few examples on using split: Useful.
Associative arrays are very useful beasts, because values tied to keys makes it so much easier to build up pseudo objects in that it makes it easier to see which piece of array data refers to what. There are a number of ways to store associative arrays of course, but this particular one below is probably one of the simplest for getting a single string out of a one level array with keys, and then transforming back into its array form when you need to make use of it again. function array_implode_with_keys($array){ $return = ”; if (count($array) > 0){ foreach ($array as $key=>$value){ $return .= $key . ‘||||’ . $value . ‘—-’; } $return = substr($return,0,strlen($return) – 4); } return $return; } function array_explode_with_keys($string){ $return = array(); $pieces = explode(‘—-’,$string); foreach($pieces as $piece){ $keyval = explode(‘||||’,$piece); if (count($keyval) > 1){ $return[$keyval[0]] = $keyval[1]; } else { $return[$keyval[0]] = ”; } } return $return; } If you look at the code above, you will see that we are making use of delimiters here to keep firstly the keys aparts, and secondly the key from its associated value. In this case we are using —- to split the array elements, and |||| to split the key from its value. In other words the resulting string is from the implode function is key1||||value1—-key2||||value2—-key3||||value3. Obviously the explode function just works in reverse.

Shame, I must say I’m quite glad that it wasn’t me who had such a disastrous start to the morning as poor old C this Friday!
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: