note 11281 modified in Array Functions by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Wed, 14 Feb 2001 19:39:25 +0000 |
| Subject: | note 11281 modified in Array Functions by jmcastagnetto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11056@lists.php.net to get a copy of this message | ||
[Editor's note: The code in the note below is a little redundant, a simpler solution would be:
// make sure there is an even number of entries
if (count($array) % 2 == 0)
for ($i=0; $i<count($array); $i += 2)
$new_array[$array[$i]] = $array[$i+1];
]
It occurred to me that there doesn't seem to be a good way to convert a normal indexed list to
an associative array. So here's a quick-n-dirty way if you're in a bind:
$array = ('key1','val1','key2','val2',...);
$s = 0;
$st = '';
$new_array = '';
foreach ($array as $v) {
if ($s == 0) {
$new_array["$v"] = $v;
$st = $v;
$s++;
} else {
$new_array["$st"] = $v;
$s = 0;
}
}
http://www.php.net/manual/array-functions.php