note 98741 modified in function.explode by danbrown

From: Date: Sun, 04 Jul 2010 14:58:52 +0000
Subject: note 98741 modified in function.explode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170693@lists.php.net to get a copy of this message
Here is a function for "double" exploding a string, like in the case of a flattened multi-dimensional array. <?php function doubleExplode ($del1, $del2, $array){ $array1 = explode("$del1", $array); foreach($array1 as $key=>$value){ $array2 = explode("$del2", $value); foreach($array2 as $key2=>$value2){ $array3[] = $value2; } } $afinal = array(); for ( $i = 0; $i <= count($array3); $i += 2) { if($array3[$i]!=""){ $afinal[trim($array3[$i])] = trim($array3[$i+1]); } } return $afinal; } $proddetails = 'Size=Large, Color=Black'; $test = doubleExplode(',', '=', $proddetails); print_r($test); ?> /* Array ( [Size] => Large [Color] => Black ) */ --was-- Here is a function for "double" exploding a string, like in the case of a flattened multi-dimensional array. <?php function doubleExplode ($del1, $del2, $array){ $array1 = explode("$del1", $array); foreach($array1 as $key=>$value){ $array2 = explode("$del2", $value); foreach($array2 as $key2=>$value2){ $array3[] = $value2; } } $afinal = array(); for ( $i = 0; $i <= count($array3); $i += 2) { if($array3[$i]!=""){ $afinal[trim($array3[$i])] = trim($array3[$i+1]); } } return $afinal; } $proddetails = 'Size=Large, Color=Black'; $test = doubleExplode(',', '=', $proddetails); print_r($test); /* Array ( [Size] => Large [Color] => Black ) */ http://php.net/manual/en/function.explode.php

« previous php.notes (#170693) next »