Multidimensional array is driving me crazy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denimderek
Forum Newbie
Posts: 7
Joined: Fri May 30, 2003 8:59 pm
Location: Corona, CA

Multidimensional array is driving me crazy

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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'),
               ),
              ),
            ),
          );
denimderek
Forum Newbie
Posts: 7
Joined: Fri May 30, 2003 8:59 pm
Location: Corona, CA

Post by denimderek »

ahh! thank you.
denimderek
Forum Newbie
Posts: 7
Joined: Fri May 30, 2003 8:59 pm
Location: Corona, CA

Post 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
                                )

                        )

                )

        )

)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

compare what I wrote to your original, you should see the differences...
denimderek
Forum Newbie
Posts: 7
Joined: Fri May 30, 2003 8:59 pm
Location: Corona, CA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
denimderek
Forum Newbie
Posts: 7
Joined: Fri May 30, 2003 8:59 pm
Location: Corona, CA

Post 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.
Post Reply