2

How to access all the elements under each key of multidimensional array.

$multi = array
(
    "Abhishek" => array("Choudhary", "Bunta", "Popy"),
    "Bond"     => array("One", "two", "three", "four"),
    "Super"    => array("T1", "T2")
);


$data = array("Abhishek","Bond","Super");           

for($j = 0;$j<count($data);$j++)
{
    echo "<br/>Main Array Value ".$data[$j]."<br/>";

    for($i = 0;$i<count($data[$j]);$i++)
    {
        echo "sub Value ".$multi[$data[$j]][$i]." count ".count($data[$j]) ;
    }
}

Now I want to iterate through each element of Abhishek , Bond and Super , so we can see Abhishek has 3 elements inside it but $data[$j] always return 1. If I increment then I can access Bunta Currently the output is -

Main Array Value Abhishek
sub Value Bunta
Main Array Value Bond
sub Value two
Main Array Value Super
sub Value T2

and expected is:

**

Main Array Value Abhishek
 sub Value choudhary
 sub Value Bunta
 sub Value Popy
:
:
Main Array Value Bond
 sub Value two
Main Array Value Super
 sub Value T2

**

Disclaimer : I am super new to PHP so may be my expectation can be invalid or I am missing some very silly thing.

5 Answers 5

3

i recommend you read some articles about multidim arrays, anyway your needs could be done with following code:

foreach($multi as $key => $value) {
    echo "<br/>Main Array Value ".$key."<br/>";
    for($i = 0; $i < sizeof($value); $i++) {
        echo "sub Value ".$value[$i]." count ".sizeof($value) ;
    }
} 

PS: you don't need $data array

Sign up to request clarification or add additional context in comments.

Comments

2

@bunta please check this out: PHP Foreach

You could use foreach instead.

Also:

PHP foreach loop through multidimensional array

HTH.

3 Comments

This supposed to be a comment since it's not giving clear answer to the question
@AlmaDoMundo Thanks, I forgot to add the link to the answer.
Link-only answers are low value on Stackoverflow.
2

Using foreach() would be easier

foreach ($multi as $key => $subarray)
{
    echo $key . '<br />';

    foreach ($subarray as $subvalue)
    {
        echo ' - '.$subvalue . '<br />';
    }
}

Will output

Abhishek
- Choudhary
- Bunta
- Popy
Bond
- One
- two
- three
- four
Super
- T1
- T2

Comments

0

Try this :

<?php

foreach($multi as $key => $value) {
    echo "<br/>Parent Value ".$key."<br/>";
    for($i = 0; $ < sizeof($value); $++) {
        echo "Child Value ".$value[$i]." count ".sizeof($value) ;
    }
}

?>

You are using wrong syntax to access it.
  • Thanks

Comments

-1

All you need is use foreach to iterate on $multi array (if you just want to know the number of subelements for each element level 1):

 <?php
echo "<br\>multiDimensional Array<br\>";
$multi = array("Abhishek" =>array
                        ("Choudhary",
                            "Bunta",
                            "Popy"),
               "Bond" => array
                           ("One",
                               "two",
                               "three",
                               "four"),
               "Super" => array
                           ("T1",
                               "T2")
);


foreach( $multi as $value ){
    echo " count ".count($value) ;
}

1 Comment

Why the downvote? I give a way to iterate and I precised that it was useful if one would wants to know the number of subelements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.