hi all,

i am new to php, i have a database field called results in that i am inserting a string something like this "category_name:-abs43;c1-abs43;types:-abs43;t1"

now i want to put this string into an array, i have used the delimiter '-abs40;' as u can see in the string.

the array should be like this Array([category_name:]=>c1[types:]=>t1)

how can i accomplish this


function toArray($str, $delimiter=',', $assoc=false)
		while(++$i < $limit)
			$temp[ $key ]=$val;
return $data;

 * Explodes the supplied string by the supplied delimiter
 * and reassembles based on each even key getting the next odd value
 * @param String $string
 * @param String $delimiter
 * @return Array
function toArray($string, $delimiter){
	$pieces = explode($delimiter, $string);
	$temp = array();
	foreach($pieces as $k => $v){
		if( ($k&1) == 0 ){
			$temp[$v] = (isset($pieces[$k+1]) ? $pieces[$k+1] : '');
	return $temp;

//Array ( [category_name:] => c1 [types:] => t1 )

At 100 iterations the difference is in the ten thousandths of a second.
At 10000 iterations its about 30% faster.

If you know for sure your array will ALWAYS have an even amount of value, removing the isset check and the ternary operator ( ?: ) will make it just over 50% faster.