Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to display each value in an associative array which is a sub array of an another large array named students. I am not able to display a particular index value using echo arrayname['key'][index].

What I have tried:

<!DOCTYPE html>
<html>
<body>
 <?php
 $students = array("Peter"=>array("35","36","31"));
 echo $students[‘Peter’][0];
 //Here 35 has to be displayed
?>
</body>
</html>
Posted
Updated 11-May-21 2:19am

1 solution

If you wish to merely display each value to the screen yet do not know exactly what is in the array use php's "foreach()" to iterate through the array.

Each time it gets a value, since you have nested arrays, you should test if that value is an array using "isarray()" and if it is the do the same thing to it.

So - if you get a value from a foreach loop, if it's an array give it a foreach loop of its own and if not then ECHO it. Do the same at each level.

I suggest you write a function that do the iteration/echo and make it recursive.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900