multidimensional array of arrays

multidimensional array of arrays

am 27.09.2007 21:58:20 von pipe.jack

I need to create multidimensional array with arrays inside of it.

database

name | value1 | value2
john | red | 45
john | red | 56
john | yellow | 11
mike | blue | 23
mike | black | 41

I would like to get this in this array format:
Array ([john] => Array( [0]=>red [1]=>red [2]=>yellow) Array( [0]=>45
[1]=>56 [2]=>11))

The idea as you can see is to have value1 and value2 as separate array
within the name array.


Thanks,
Jack

Re: multidimensional array of arrays

am 27.09.2007 22:13:17 von ragearc

On 27 Sep, 20:58, JackpipE wrote:
> I need to create multidimensional array with arrays inside of it.
>
> database
>
> name | value1 | value2
> john | red | 45
> john | red | 56
> john | yellow | 11
> mike | blue | 23
> mike | black | 41
>
> I would like to get this in this array format:
> Array ([john] => Array( [0]=>red [1]=>red [2]=>yellow) Array( [0]=>45
> [1]=>56 [2]=>11))
>
> The idea as you can see is to have value1 and value2 as separate array
> within the name array.
>
> Thanks,
> Jack

$array = array(
'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
[...]
);

usage example:

print $array['JOHN']['VALUE1']; // Outputs red.

Re: multidimensional array of arrays

am 27.09.2007 22:27:36 von Steve

"RageARC" wrote in message
news:1190923997.302334.233810@g4g2000hsf.googlegroups.com...
> On 27 Sep, 20:58, JackpipE wrote:


>> name | value1 | value2
>> john | red | 45
>> john | red | 56
>> john | yellow | 11
>> mike | blue | 23
>> mike | black | 41

> $array = array(
> 'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
> [...]
> );

actually, no, not based on his data.

$array['john']['red'] = array(45, 56);
$array['john']['yellow'] = array(11);
$array['mike']['blue'] = array(23);
$array['mike']['black'] = array(41);

to be completely literal about it. if coming from a db...

$array = array();
foreach ($records as $record)
{
$array[$record['PERSON']][$record['COLOR'][] = $record['NUMBER'];
}

would build what we did by hand above. useage:

foreach ($array as $person => $colors)
{
echo '

+' . $person . '
';
foreach ($colors as $color => $numbers)
{
echo '
  -- ' . $color. '
';
foreach ($numbers as $number)
{
echo '
    -- ' . $number. '
';
}
}
}

Re: multidimensional array of arrays

am 27.09.2007 22:33:38 von ragearc

On 27 Sep, 21:27, "Steve" wrote:
> "RageARC" wrote in message
>
> news:1190923997.302334.233810@g4g2000hsf.googlegroups.com...
>
> > On 27 Sep, 20:58, JackpipE wrote:
> >> name | value1 | value2
> >> john | red | 45
> >> john | red | 56
> >> john | yellow | 11
> >> mike | blue | 23
> >> mike | black | 41
> > $array = array(
> > 'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
> > [...]
> > );
>
> actually, no, not based on his data.
>
> $array['john']['red'] = array(45, 56);
> $array['john']['yellow'] = array(11);
> $array['mike']['blue'] = array(23);
> $array['mike']['black'] = array(41);
>
> to be completely literal about it. if coming from a db...
>
> $array = array();
> foreach ($records as $record)
> {
> $array[$record['PERSON']][$record['COLOR'][] = $record['NUMBER'];
>
> }
>
> would build what we did by hand above. useage:
>
> foreach ($array as $person => $colors)
> {
> echo '

+' . $person . '
';
> foreach ($colors as $color => $numbers)
> {
> echo '
  -- ' . $color. '
';
> foreach ($numbers as $number)
> {
> echo '
    -- ' . $number. '
';
> }
> }
>
> }
>
>

Sorry, but then we have different interpretations of the same.

> The idea as you can see is to have value1 and value2 as separate array
> within the name array.

Inside NAME, one must have an array with VALUE1 and another with
VALUE2. I admit that rereading now made it clearer for me. Let me
reformulate:

$array = array(
'john' => array(
'VALUE1' => array('red'),
'VALUE2' => array('45','56')
),
[...,]
);

Your way is similar to mine, but my second value is not inside the
first value ;) They are in different arrays.

Re: multidimensional array of arrays

am 27.09.2007 22:50:34 von Steve

"RageARC" wrote in message
news:1190925218.208985.247230@k79g2000hse.googlegroups.com.. .
> On 27 Sep, 21:27, "Steve" wrote:
>> "RageARC" wrote in message
>>
>> news:1190923997.302334.233810@g4g2000hsf.googlegroups.com...
>>
>> > On 27 Sep, 20:58, JackpipE wrote:
>> >> name | value1 | value2
>> >> john | red | 45
>> >> john | red | 56
>> >> john | yellow | 11
>> >> mike | blue | 23
>> >> mike | black | 41
>> > $array = array(
>> > 'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
>> > [...]
>> > );
>>
>> actually, no, not based on his data.
>>
>> $array['john']['red'] = array(45, 56);
>> $array['john']['yellow'] = array(11);
>> $array['mike']['blue'] = array(23);
>> $array['mike']['black'] = array(41);
>>
>> to be completely literal about it. if coming from a db...
>>
>> $array = array();
>> foreach ($records as $record)
>> {
>> $array[$record['PERSON']][$record['COLOR'][] = $record['NUMBER'];
>>
>> }
>>
>> would build what we did by hand above. useage:
>>
>> foreach ($array as $person => $colors)
>> {
>> echo '

+' . $person . '
';
>> foreach ($colors as $color => $numbers)
>> {
>> echo '
  -- ' . $color. '
';
>> foreach ($numbers as $number)
>> {
>> echo '
    -- ' . $number. '
';
>> }
>> }
>>
>> }
>>
>>
>
> Sorry, but then we have different interpretations of the same.

yes...theres mine...which works. and yours...which doesn't correlate to the
sample data he gave...at all.

;^)

>> The idea as you can see is to have value1 and value2 as separate array
>> within the name array.

which may be well and good, however when you left out the fact that john
also likes yellow. further, you provide no direct association from the
values to the colors...unless you FORCE a for (...) iterator construct to
magically link them. shit, that's going to hurt when one of either the
parent or children are deleted from the array! you'll be looking to iterate
over sequencial keys...that could actually end up with gaping holes,
thinking, 'why the hell won't this work!'

> Inside NAME, one must have an array with VALUE1 and another with
> VALUE2. I admit that rereading now made it clearer for me. Let me
> reformulate:
>
> $array = array(
> 'john' => array(
> 'VALUE1' => array('red'),
> 'VALUE2' => array('45','56')
> ),
> [...,]
> );

yes, yes...now, reformulate again...to something that will work without
being contrived. ;^)

> Your way is similar to mine, but my second value is not inside the
> first value ;) They are in different arrays.

again, perhaps similar, but the important thing is...mine works. please,
provide your working example using EXACTLY this 're'formulation.

Re: multidimensional array of arrays

am 27.09.2007 23:17:51 von ragearc

On 27 Sep, 21:50, "Steve" wrote:
> "RageARC" wrote in message
>
> news:1190925218.208985.247230@k79g2000hse.googlegroups.com.. .
>
>
>
> > On 27 Sep, 21:27, "Steve" wrote:
> >> "RageARC" wrote in message
>
> >>news:1190923997.302334.233810@g4g2000hsf.googlegroups.com. ..
>
> >> > On 27 Sep, 20:58, JackpipE wrote:
> >> >> name | value1 | value2
> >> >> john | red | 45
> >> >> john | red | 56
> >> >> john | yellow | 11
> >> >> mike | blue | 23
> >> >> mike | black | 41
> >> > $array = array(
> >> > 'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
> >> > [...]
> >> > );
>
> >> actually, no, not based on his data.
>
> >> $array['john']['red'] = array(45, 56);
> >> $array['john']['yellow'] = array(11);
> >> $array['mike']['blue'] = array(23);
> >> $array['mike']['black'] = array(41);
>
> >> to be completely literal about it. if coming from a db...
>
> >> $array = array();
> >> foreach ($records as $record)
> >> {
> >> $array[$record['PERSON']][$record['COLOR'][] = $record['NUMBER'];
>
> >> }
>
> >> would build what we did by hand above. useage:
>
> >> foreach ($array as $person => $colors)
> >> {
> >> echo '

+' . $person . '
';
> >> foreach ($colors as $color => $numbers)
> >> {
> >> echo '
  -- ' . $color. '
';
> >> foreach ($numbers as $number)
> >> {
> >> echo '
    -- ' . $number. '
';
> >> }
> >> }
>
> >> }
>
> > Sorry, but then we have different interpretations of the same.
>
> yes...theres mine...which works. and yours...which doesn't correlate to the
> sample data he gave...at all.
>
> ;^)
>
> >> The idea as you can see is to have value1 and value2 as separate array
> >> within the name array.
>
> which may be well and good, however when you left out the fact that john
> also likes yellow. further, you provide no direct association from the
> values to the colors...unless you FORCE a for (...) iterator construct to
> magically link them. shit, that's going to hurt when one of either the
> parent or children are deleted from the array! you'll be looking to iterate
> over sequencial keys...that could actually end up with gaping holes,
> thinking, 'why the hell won't this work!'
>
> > Inside NAME, one must have an array with VALUE1 and another with
> > VALUE2. I admit that rereading now made it clearer for me. Let me
> > reformulate:
>
> > $array = array(
> > 'john' => array(
> > 'VALUE1' => array('red'),
> > 'VALUE2' => array('45','56')
> > ),
> > [...,]
> > );
>
> yes, yes...now, reformulate again...to something that will work without
> being contrived. ;^)
>
> > Your way is similar to mine, but my second value is not inside the
> > first value ;) They are in different arrays.
>
> again, perhaps similar, but the important thing is...mine works. please,
> provide your working example using EXACTLY this 're'formulation.

foreach ($array['JOHN']['VALUE2'] as $value2) {
print $value2;
}

print $array['JOHN']['VALUE1'][0];
print $array['JOHN']['VALUE2'][0];

Re: multidimensional array of arrays

am 27.09.2007 23:23:23 von ragearc

> which may be well and good, however when you left out the fact that john
> also likes yellow. further, you provide no direct association from the
> values to the colors...unless you FORCE a for (...) iterator construct to
> magically link them. shit, that's going to hurt when one of either the
> parent or children are deleted from the array! you'll be looking to iterate
> over sequencial keys...that could actually end up with gaping holes,
> thinking, 'why the hell won't this work!'

Yes you are right, I just seen that part :S. Damn I gotta learn to
read things carefully x).

Yep yours is the best ;). I surrender. Take the bike lol.

Re: multidimensional array of arrays

am 27.09.2007 23:27:05 von Steve

"RageARC" wrote in message
news:1190927871.534459.223320@22g2000hsm.googlegroups.com...
> On 27 Sep, 21:50, "Steve" wrote:
>> "RageARC" wrote in message
>>
>> news:1190925218.208985.247230@k79g2000hse.googlegroups.com.. .
>>
>>
>>
>> > On 27 Sep, 21:27, "Steve" wrote:
>> >> "RageARC" wrote in message
>>
>> >>news:1190923997.302334.233810@g4g2000hsf.googlegroups.com. ..
>>
>> >> > On 27 Sep, 20:58, JackpipE wrote:
>> >> >> name | value1 | value2
>> >> >> john | red | 45
>> >> >> john | red | 56
>> >> >> john | yellow | 11
>> >> >> mike | blue | 23
>> >> >> mike | black | 41
>> >> > $array = array(
>> >> > 'john' => array('VALUE1' => 'red', 'VALUE2' => '45'),
>> >> > [...]
>> >> > );
>>
>> >> actually, no, not based on his data.
>>
>> >> $array['john']['red'] = array(45, 56);
>> >> $array['john']['yellow'] = array(11);
>> >> $array['mike']['blue'] = array(23);
>> >> $array['mike']['black'] = array(41);
>>
>> >> to be completely literal about it. if coming from a db...
>>
>> >> $array = array();
>> >> foreach ($records as $record)
>> >> {
>> >> $array[$record['PERSON']][$record['COLOR'][] = $record['NUMBER'];
>>
>> >> }
>>
>> >> would build what we did by hand above. useage:
>>
>> >> foreach ($array as $person => $colors)
>> >> {
>> >> echo '

+' . $person . '
';
>> >> foreach ($colors as $color => $numbers)
>> >> {
>> >> echo '
  -- ' . $color. '
';
>> >> foreach ($numbers as $number)
>> >> {
>> >> echo '
    -- ' . $number. '
';
>> >> }
>> >> }
>>
>> >> }
>>
>> > Sorry, but then we have different interpretations of the same.
>>
>> yes...theres mine...which works. and yours...which doesn't correlate to
>> the
>> sample data he gave...at all.
>>
>> ;^)
>>
>> >> The idea as you can see is to have value1 and value2 as separate array
>> >> within the name array.
>>
>> which may be well and good, however when you left out the fact that john
>> also likes yellow. further, you provide no direct association from the
>> values to the colors...unless you FORCE a for (...) iterator construct to
>> magically link them. shit, that's going to hurt when one of either the
>> parent or children are deleted from the array! you'll be looking to
>> iterate
>> over sequencial keys...that could actually end up with gaping holes,
>> thinking, 'why the hell won't this work!'
>>
>> > Inside NAME, one must have an array with VALUE1 and another with
>> > VALUE2. I admit that rereading now made it clearer for me. Let me
>> > reformulate:
>>
>> > $array = array(
>> > 'john' => array(
>> > 'VALUE1' => array('red'),
>> > 'VALUE2' => array('45','56')
>> > ),
>> > [...,]
>> > );
>>
>> yes, yes...now, reformulate again...to something that will work without
>> being contrived. ;^)
>>
>> > Your way is similar to mine, but my second value is not inside the
>> > first value ;) They are in different arrays.
>>
>> again, perhaps similar, but the important thing is...mine works. please,
>> provide your working example using EXACTLY this 're'formulation.
>
> foreach ($array['JOHN']['VALUE2'] as $value2) {
> print $value2;
> }
>
> print $array['JOHN']['VALUE1'][0];
> print $array['JOHN']['VALUE2'][0];

no, no...using his data...ALL of it. then iterate the array cleanly showing:

john
red
45
56
yellow
11
mike
blue
23
black
41

what you've shown is the sample data YOU wanted. the op doesn't know how to
set up HIS data properly into an array. YOUR data and example will lead to
HIS code not working when implemented.

now, try again.

Re: multidimensional array of arrays

am 27.09.2007 23:31:41 von Steve

"RageARC" wrote in message
news:1190928203.023905.249060@22g2000hsm.googlegroups.com...
>> which may be well and good, however when you left out the fact that john
>> also likes yellow. further, you provide no direct association from the
>> values to the colors...unless you FORCE a for (...) iterator construct to
>> magically link them. shit, that's going to hurt when one of either the
>> parent or children are deleted from the array! you'll be looking to
>> iterate
>> over sequencial keys...that could actually end up with gaping holes,
>> thinking, 'why the hell won't this work!'
>
> Yes you are right, I just seen that part :S. Damn I gotta learn to
> read things carefully x).
>
> Yep yours is the best ;). I surrender. Take the bike lol.

i didn't want confrontation. i just wanted the op to see how to cleanly
setup his data...and for you to understand why yours didn't quite get there.

cheers

Re: multidimensional array of arrays

am 27.09.2007 23:36:36 von ragearc

Yep now I see, with the array setup that way :)

$array['JOHN']['RED'][] = 45;
$array['JOHN']['RED'][] = 56;
$array['JOHN']['YELLOW'][] = 11;

This certainly works ;)