note 11281 deleted from ref.array by jimw
| From: | jimw@php.net | Date: | Sat, 15 Dec 2001 22:33:11 +0000 |
| Subject: | note 11281 deleted from ref.array by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22912@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;
}
}