Re: note 11281 added to Array Functions
| From: | Jesus M. Castagnetto | Date: | Wed, 14 Feb 2001 19:39:34 +0000 |
| Subject: | Re: note 11281 added to Array Functions | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-11055@lists.php.net to get a copy of this message | ||
I think that your code is a little redundant whem
generating the array: key1=>val1, key2=>val2, etc.
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];
I will add this comment to your contribution.
Cheers
--- nospamjong@jong.org wrote:
> 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/en/ref.array.php
>
>
> --
> PHP Notes Mailing List (http://www.php.net/)
> To unsubscribe, e-mail:
> php-notes-unsubscribe@lists.php.net
> For additional commands, e-mail:
> php-notes-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
=====
--- Jesus M. Castagnetto <jmcastagnetto@php.net>
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/