Array Question

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?

Thanks in advance


       $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"
          ));




GiantMatrixAsked:
Who is Participating?
 
BogoJokerConnect With a Mentor Commented:
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
 
BogoJokerCommented:
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
 
Harisha M GCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
BogoJokerCommented:
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
 
Harisha M GCommented:
Sorry Joe, I overlooked that line :-|
0
 
BogoJokerCommented:
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
 
GiantMatrixAuthor Commented:
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?

Thanks in advance
0
 
Harisha M GCommented:
<?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
 
BogoJokerCommented:
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
 
BogoJokerCommented:
<?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 'Your Method:<br><pre>';
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
 
Harisha M GCommented:
Joe, if you push the elements, you won't get the "associative" array.

So, why not declare the array directly ?
0
 
GiantMatrixAuthor Commented:
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?

Thanks in advance for your help

$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
 
BogoJokerCommented:
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
 
GiantMatrixAuthor Commented:
Thanks Joe for your outstanding help and support
0
 
BogoJokerCommented:
Sure thing =)
I learned something as well.  Thanks mgh ;)

Joe P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.