Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #11776 > unrolled thread
| Started by | nawfer <novalidsforspam@alt.al> |
|---|---|
| First post | 2013-11-15 13:23 +0100 |
| Last post | 2013-11-15 17:33 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.php
how change an array nawfer <novalidsforspam@alt.al> - 2013-11-15 13:23 +0100
Re: how change an array Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-15 07:47 -0500
Re: how change an array Norman Peelman <npeelman@cfl.rr.com> - 2013-11-15 08:03 -0500
Re: how change an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-15 15:11 +0100
Re: how change an array nawfer <novalidsforspam@alt.al> - 2013-11-15 17:32 +0100
Re: how change an array Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-15 17:33 +0000
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-11-15 13:23 +0100 |
| Subject | how change an array |
| Message-ID | <w24nnxz85z5i$.108876h4m2t3t.dlg@40tude.net> |
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 )
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-15 07:47 -0500 |
| Message-ID | <l6555n$1tq$1@dont-email.me> |
| In reply to | #11776 |
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@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2013-11-15 08:03 -0500 |
| Message-ID | <l6562m$867$1@dont-email.me> |
| In reply to | #11776 |
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?-
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-11-15 15:11 +0100 |
| Message-ID | <3677958.dk8vouHeg2@PointedEars.de> |
| In reply to | #11778 |
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
[toc] | [prev] | [next] | [standalone]
| From | nawfer <novalidsforspam@alt.al> |
|---|---|
| Date | 2013-11-15 17:32 +0100 |
| Message-ID | <1xxm4yfydjkhr.96fn6ypd0iug$.dlg@40tude.net> |
| In reply to | #11778 |
> foreach($models as $md)
> {
> $articles[$md->id] = $md->name;
> }
thanks
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-15 17:33 +0000 |
| Message-ID | <l65lt0$769$2@dont-email.me> |
| In reply to | #11776 |
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@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web