FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how change an array
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
how change an array [message #183700] Fri, 15 November 2013 12:23 Go to next message
nawfer is currently offline  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 #183701 is a reply to message #183700] Fri, 15 November 2013 12:47 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2013 7:23 AM, 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 )
>

What have you tried so far?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: how change an array [message #183702 is a reply to message #183700] Fri, 15 November 2013 13:03 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2013 07:23 AM, 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 )
>


foreach($models as $md)
{
$articles[$md->id] = $md->name;
}

or (if above doesn't work)

foreach($models as $md)
{
$tmp = $md->id;
$articles[$tmp] = $md->name;
}


--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: how change an array [message #183703 is a reply to message #183702] Fri, 15 November 2013 14:11 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Norman Peelman wrote:

> On 11/15/2013 07:23 AM, 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 )
>>
>
> foreach($models as $md)
> {
> $articles[$md->id] = $md->name;
> }
>
> or (if above doesn't work)
> foreach($models as $md)
> {
> $tmp = $md->id;
> $articles[$tmp] = $md->name;
> }

Under which circumstances would the second code work, but not the first one?

BTW: You should be writing “foreach (” (and “if (”, “switch (” etc.) so that
it is not mistaken by the reader for a function call.


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
Re: how change an array [message #183704 is a reply to message #183702] Fri, 15 November 2013 16:32 Go to previous messageGo to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
> foreach($models as $md)
> {
> $articles[$md->id] = $md->name;
> }

thanks
Re: how change an array [message #183706 is a reply to message #183700] Fri, 15 November 2013 17:33 Go to previous message
Denis McMahon is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IT company looking for skilled resources
Next Topic: detecting refresh versus initial page load.
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Dec 27 03:40:23 GMT 2024

Total time taken to generate the page: 0.09738 seconds