Home »
Imported messages »
comp.lang.php »
how change an array
how change an array [message #183700] |
Fri, 15 November 2013 12:23  |
nawfer
Messages: 34 Registered: August 2011
Karma: 0
|
Member |
|
|
with this
1)
foreach($models as $md)
{$articles[] = array($md->id,$md->name);}
I have
Array ([0] => Array ([0] => 4 [1] => Libri )
[1] => Array ( [0] => 2 [1] => Riviste )
[2] => Array ( [0] => 1 [1] => Giornali ) )
how can change code 1) for have this:
Array ( [4] => Libri
[2] => Riviste
[1] => Giornali )
|
|
|
|
|
|
|
Re: how change an array [message #183706 is a reply to message #183700] |
Fri, 15 November 2013 17:33  |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Fri, 15 Nov 2013 13:23:45 +0100, nawfer wrote:
> with this 1)
> foreach($models as $md)
> {$articles[] = array($md->id,$md->name);}
>
> I have Array ([0] => Array ([0] => 4 [1] => Libri )
> [1] => Array ( [0] => 2 [1] => Riviste ) [2] => Array ( [0] => 1
> [1] => Giornali ) )
>
>
> how can change code 1) for have this:
>
> Array ( [4] => Libri
> [2] => Riviste
> [1] => Giornali )
Looks like you have an array of sub arrays, and want to change it to an
associative array where sub array element 1 is the key and sub array
element 2 is the data.
I'd guess you want to define a new associative array, then step through
the existing array assigning elements in the new associative array
according to the values in each sub array of the existing array.
You might want to do some sort of sanity check before you start that none
of the sub arrays in the existing array have the same values in the key
element, as otherwise you may end up losing data.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Goto Forum:
Current Time: Fri Feb 28 11:33:05 GMT 2025
Total time taken to generate the page: 0.05758 seconds