[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

Array Question

Posted on 2006-05-06
Medium Priority
345 Views
Hi All,

Given the code shown below, how I can get  ( \$FirstArray['a'] ) value if I want to read this value using the path ThirdArray >> SecondArray >> FirstArray?

\$FirstArray = array();
array_push(\$FirstArray, array(
'a' => "A",
'b' => "B"
));

\$SecondArray = array();
array_push(\$SecondArray, array(
'a2' => \$FirstArray,
'b2' => "B2"
));

\$ThirdArray = array();
array_push(\$ThirdArray, array(
'a3' => \$SecondArray,
'b3' => "B3"
));

0
Question by:GiantMatrix
• 8
• 4
• 3

LVL 17

Expert Comment

ID: 16623715
Hi GiantMatrix,

Here is an example.
<?php
\$FirstArray = array();
array_push(\$FirstArray, array(
'a' => "A",
'b' => "B"
));

\$SecondArray = array();
array_push(\$SecondArray, array(
'a2' => \$FirstArray,
'b2' => "B2"
));

\$ThirdArray = array();
array_push(\$ThirdArray, array(
'a3' => \$SecondArray,
'b3' => "B3"
));

print_r(\$ThirdArray);
print '<br>';
print \$FirstArray[0]['a'];
print '<br>';
print \$SecondArray[0]['a2'][0]['a'];
print '<br>';
print \$ThirdArray[0]['a3'][0]['a2'][0]['a'];
?>

Your nesting a lot of arrays inside arrays, and because some of them you don't give an associated index, they default to 0 (then 1, 2, 3....)

The bottom three will all print: A

Joe P
0

LVL 37

Expert Comment

ID: 16623836
Hi, the best way to know what index contains what is to print that array using print_r or var_dump...

print_r(\$ThirdArray);

or

var_dump(\$ThirdArray);

Note: If you are printing this in an HTML file, then use the <PRE> tag to align the output correctly..

echo "<PRE>";
print_r(\$ThirdArray);
echo "</PRE>";

Using this, you can see the contents of \$_POST, \$_GET variables, arrays or other variables.

---
Harish
0

LVL 17

Expert Comment

ID: 16623837
I did put a print_r(\$ThirdArray) in my code for just that reason.
But mgh brings up a couple good points.

Joe P
0

LVL 37

Expert Comment

ID: 16623841
Sorry Joe, I overlooked that line :-|
0

LVL 17

Expert Comment

ID: 16623845
No problem, I didn't explain why I put it and I am sure it would help GiantMatrix if we explained it a little more.

I used that print_r() to see exactly how the \$ThirdArray was built.  I used that informatio for the following three prints.  I learned something from mgh's last post, using the <pre> would make it much easier to see how the array is built!!  I never knew that, I always thought it was just spaces. =)

Joe P
0

Author Comment

ID: 16625117
Hi Joe,

You said "and because some of them you don't give an associated index, they default to 0 (then 1, 2, 3....)".
So can you please tell me how I can make an associated index in those arrays?

Also I tried doing something like this:
print Count(\$ThirdArray);

and I expected to have 2 displayed, but instead I found 1 ....so is this normal?

0

LVL 37

Expert Comment

ID: 16625181
<?php
\$FirstArray= array(
'a' => "A",
'b' => "B"
);

\$SecondArray = array(
'a2' => \$FirstArray,
'b2' => "B2"
);

\$ThirdArray= array(
'a3' => \$SecondArray,
'b3' => "B3"
);

echo "<PRE>\n";
print_r(\$ThirdArray);
echo "\n</PRE>";
?>
0

LVL 17

Expert Comment

ID: 16625201
Let me explain the entire situation:

1.
\$FirstArray = array();
array_push(\$FirstArray, array(
'a' => "A",
'b' => "B"
));

Here you are pushing an array onto \$FirstArray.  Because there is no defined associated index, it defaults to 0.  Therefore \$FirstArray[0] will be an array with "A", and "B".  So you have 1 element in \$FirstArray, that element is an array!!  That array has two elements "A" and "B"

2.
\$SecondArray = array();
array_push(\$SecondArray, array(
'a2' => \$FirstArray,
'b2' => "B2"
));

Same thing, your pushing an array onto \$SecondArray in the 0 position.  Again the count is one, \$SecondArray has 1 element that is an array.  That array is itself multidimensional.  That array has two indexs, a2 and b2.  a2 is an array, b2 is just an element.

3.
\$ThirdArray = array();
array_push(\$ThirdArray, array(
'a3' => \$SecondArray,
'b3' => "B3"
));

The third time you are inserting an array in the 0 position again.  This array is again complex just like \$SecondArray.  Notice that \$ThirdArray indeed has ONE element.  That one element is an array!  So again the structure of this is [0] holds an array where [a3] is \$SecondArray and [b3] is the element B3.  Then you would have to cycle through SecondArray by starting with [0] then [a2] or [b2] for that element.  The cycle continues.

The key issue here is that your pushing arrays into arrays and causing that unecessary [0] element.
Take this situation for example (I am sticking with the array_push only because I want to keep the same flow)
(I will make it another post so it is cleaner)

Joe P
0

LVL 17

Expert Comment

ID: 16625206
<?php
\$FirstArray = array();
array_push(\$FirstArray, array(
'a' => "A",
'b' => "B"
));

\$SecondArray = array();
array_push(\$SecondArray, array(
'a2' => \$FirstArray,
'b2' => "B2"
));

\$ThirdArray = array();
array_push(\$ThirdArray, array(
'a3' => \$SecondArray,
'b3' => "B3"
));
print_r(\$ThirdArray);
print '</pre>';
// Will print AAA
print \$FirstArray[0]['a'];
print \$SecondArray[0]['a2'][0]['a'];
print \$ThirdArray[0]['a3'][0]['a2'][0]['a'];

// My New Code
\$first = array();
array_push(\$first, 'A', 'B');
\$second = array();
array_push(\$second, \$first, 'B2');
\$third = array();
array_push(\$third, \$second, 'B3');
print '<br>My Method:<br><pre>';
print_r(\$third);
print '</pre>';
// Will print AAA
print \$first[0];
print\$second[0][0];
print \$third[0][0][0];
?>

Here the major difference is I am not pushing arrays onto arrays.  I am pushing elements onto the arrays.  Pushing will always put the element in sub 0, sub 1 also known as [0] [1], counting up sequentially.

I come from a java background and I am comfortable with this syntax but I actually perfer giving them names!  For that though I don't think you can do that with array_push() or any other method, you actually need to do the assigning which is not hard at all.

Joe P
0

LVL 37

Expert Comment

ID: 16625216
Joe, if you push the elements, you won't get the "associative" array.

So, why not declare the array directly ?
0

LVL 17

Accepted Solution

BogoJoker earned 2000 total points
ID: 16625241
I said I would keep with the array_push() implementation that GiantMatrix was using before.
If he wants assoiatice indexs (indexs with words that are easier for you to remember) then either of us can provide him with that because it is the most common way.  array_push() is not often used so I figured he had a specific use.

Just the same, if you are going to stick with array_push() and you are pushing one element like your above situations.  You can actually use a cool php syntax, like this:
\$anArray = array();
\$anArray[] = 'A';  // This sets \$anArray[0] to 'A'
\$anotherArray = array();
\$anotherArray[] = array('A', 'B');  // This sets \$anotherArray[0] to an array, so \$anotherArray[0][0] is 'A'

If you want indexs with words rather then this 0, 1, 2 stuff.
\$first['a'] = 'A';
\$first['b'] = 'B';
\$second['first'] = \$first;
\$second['b2'] = 'B2';f
\$third['second'] = \$second;
\$third['b3'] = 'B3';

Now:
\$third['second']['first']['a'] is A
\$third['second']['b2'] is B2
And so on.
0

Author Comment

ID: 16626891
Hi Joe,

Thank you so much for your outstanding help and support.

Concerning array_push, I really have no specific reason to use it this function specially. So I think I will stick with you way shown below in order to have nested "associative arrays".

Although my last question here will be, is this the best way in order to have nested associative arrays?

\$first['a'] = 'A';
\$first['b'] = 'B';
\$second['first'] = \$first;
\$second['b2'] = 'B2';f
\$third['second'] = \$second;
\$third['b3'] = 'B3';

Now:
\$third['second']['first']['a'] is A
\$third['second']['b2'] is B2
And so on.
0

LVL 17

Expert Comment

ID: 16626924
Is it the best way?  I am not sure.

The optimum way I guess would be having it all right in one single array! =)
But since you have multiple dimensional arrays (ararys in arrays) I don't really see any other way to do it.
Of course you can change the names to anything that you want, that will help you remember.  The use of those associative arrays is to make it easy on use humans =)

Joe P
0

Author Comment

ID: 16626981
Thanks Joe for your outstanding help and support
0

LVL 17

Expert Comment

ID: 16626986
Sure thing =)
I learned something as well.  Thanks mgh ;)

Joe P
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult toâ€¦
Itâ€™s a season to be thankful, and weâ€™re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creatâ€¦
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll