Page 1 of 1

Multidimensional array is driving me crazy

Posted: Sun Sep 12, 2004 1:08 pm
by denimderek
I'm trying to build a multidimensional array of products. The main array's KEY name is PRODUCTS. Within products there are RECORDS and CLOTHES. Within RECORDS there are SIZE and COLOR arrays. Within CLOTHES array, there is a TSHIRTS array. Within the TSHIRTS array, there are SIZE and COLOR arrays. My array looks like this:

Code: Select all

$array = array('PRODUCTS' => 
				array('RECORDS' => 
					array('SIZE' => array('7INCH','10INCH','12INCH','OTHER')),
					array('COLOR' => array('BLACK','WHITE','GREY'))
				),
				array('CLOTHES' =>
					array('T-SHIRTS' =>
						array('SIZE' => array('SMALL','MEDIUM','LARGE')),
						array('COLOR' => array('BLACK','WHITE','GREY'))
					)
				)
		 	);
But a print_r($array) shows that the array COLOR array (both inside the RECORDS and TSHIRTS arrays) are outside of the arrays which I'm intending on being their parent. Take a look:

Code: Select all

Array
(
    їPRODUCTS] => Array
        (
            їRECORDS] => Array
                (
                    їSIZE] => Array
                        (
                            ї0] => 7INCH
                            ї1] => 10INCH
                            ї2] => 12INCH
                            ї3] => OTHER
                        )

                )

            ї0] => Array
                (
                    їCOLOR] => Array
                        (
                            ї0] => BLACK
                            ї1] => WHITE
                            ї2] => GREY
                        )

                )

        )

    ї0] => Array
        (
            їCLOTHES] => Array
                (
                    їT-SHIRTS] => Array
                        (
                            їSIZE] => Array
                                (
                                    ї0] => SMALL
                                    ї1] => MEDIUM
                                    ї2] => LARGE
                                )

                        )

                    ї0] => Array
                        (
                            їCOLOR] => Array
                                (
                                    ї0] => BLACK
                                    ї1] => WHITE
                                    ї2] => GREY
                                )

                        )

                )

        )

)
I'm sure this is a matter of me putting a parentheses in the wrong place, but I can't figure out where for the life of me. Anyone? Help?

Posted: Sun Sep 12, 2004 1:13 pm
by feyd

Code: Select all

$array = array('PRODUCTS' =>
            array('RECORDS' => array(
               'SIZE' => array('7INCH','10INCH','12INCH','OTHER'),
               'COLOR' => array('BLACK','WHITE','GREY'),
              ),
            ),
            array('CLOTHES' =>
               array('T-SHIRTS' => array(
                  'SIZE' => array('SMALL','MEDIUM','LARGE'),
                  'COLOR' => array('BLACK','WHITE','GREY'),
               ),
              ),
            ),
          );

Posted: Sun Sep 12, 2004 1:25 pm
by denimderek
ahh! thank you.

Posted: Sun Sep 12, 2004 1:49 pm
by denimderek
feyd wrote:

Code: Select all

$array = array('PRODUCTS' =>
            array('RECORDS' => array(
               'SIZE' => array('7INCH','10INCH','12INCH','OTHER'),
               'COLOR' => array('BLACK','WHITE','GREY'),
              ),
            ),
            array('CLOTHES' =>
               array('T-SHIRTS' => array(
                  'SIZE' => array('SMALL','MEDIUM','LARGE'),
                  'COLOR' => array('BLACK','WHITE','GREY'),
               ),
              ),
            ),
          );
I spoke too soon. That is ALMOST right. the CLOTHES array is not within the PRODUCTS array though:

Code: Select all

Array
(
    [PRODUCTS] => Array
        (
            [RECORDS] => Array
                (
                    [SIZE] => Array
                        (
                            [0] => 7INCH
                            [1] => 10INCH
                            [2] => 12INCH
                            [3] => OTHER
                        )

                    [COLOR] => Array
                        (
                            [0] => BLACK
                            [1] => WHITE
                            [2] => GREY
                        )

                )

        )

    [0] => Array
        (
            [CLOTHES] => Array
                (
                    [T-SHIRTS] => Array
                        (
                            [SIZE] => Array
                                (
                                    [0] => SMALL
                                    [1] => MEDIUM
                                    [2] => LARGE
                                )

                            [COLOR] => Array
                                (
                                    [0] => BLACK
                                    [1] => WHITE
                                    [2] => GREY
                                )

                        )

                )

        )

)

Posted: Sun Sep 12, 2004 1:59 pm
by feyd
compare what I wrote to your original, you should see the differences...

Posted: Sun Sep 12, 2004 2:11 pm
by denimderek
Here is the code you gave me:

Code: Select all

$array = array('PRODUCTS' => 
			array('RECORDS' => array(
				'SIZE' => array('7INCH','10INCH','12INCH','OTHER'),
				'COLOR' => array('BLACK','WHITE','GREY'),
				),
			),
			array('CLOTHES' =>
				array('T-SHIRTS' => array(
					'SIZE' => array('SMALL','MEDIUM','LARGE'),
					'COLOR' => array('BLACK','WHITE','GREY'),
					),
				),
			),
		);
Here is what I'm trying to print:

Code: Select all

echo "{$array[PRODUCTS][RECORDS][COLOR][0]}\n";
echo "<br><br>";
echo "{$array[PRODUCTS][CLOTHES][T-SHIRTS][COLOR][0]}\n";
This should print the following:
BLACK
BLACK
But all that is printed is one single "BLACK" ... which of course is the BLACK from the RECORDS -> COLOR array, not the CLOTHES -> T-SHIRTS -> COLOR array. Right now the CLOTHES array is not part of the PRODUCTS array. Any clue?

Posted: Sun Sep 12, 2004 2:33 pm
by feyd
I'm not here to give you all the answers.. compare the code I posted, to the code you originally posted. Find the fundamental differences.. that's the answer.

Posted: Sun Sep 12, 2004 2:35 pm
by John Cartwright
Look carefully at the amount of arrays and amount of closing brackets, try echo'ing out things like $array['PRODUCT']['CLOTHES'] ... you'll notiuce that its not even getting that, so the problem lies in the first sets of arrays. The problem is right infront of you. :P

Posted: Sun Sep 12, 2004 3:07 pm
by denimderek
Thanks for being tough on me :wink:

Code: Select all

$array = array('PRODUCTS' => array(
					'RECORDS' => array(
						'SIZE' =>array('7INCH','10INCH','12INCH','OTHER'),
						'COLOR' => array('BLACK','WHITE','GREY'),
						),
					'CLOTHES' => array(
			   			'TSHIRTS' => array(
							'SIZE' => array('SMALL','MEDIUM','LARGE'),
							'COLOR' => array('BLACK','WHITE','GREY'),
							),
						),
					),
				);
Also, I learned its not a wise idea to hyphenate an array key without escaping the hyphen. That through me for a bit of a loop. Dur.