1
<?php
$p_23 = array("Name"=>"XYZ","Age"=>"12");
$a_23 = array("Class"=>"5","Sec"=>"A");
$r_23 = array("Personal"=>$p_23,"Academic"=>$a_23);

$p_24 = array("Name"=>"ABC","Age"=>"14");
$a_24 = array("Class"=>"6","Sec"=>"B");
$r_24 = array("Personal"=>$p_24,"Academic"=>$a_24);

$stud = array("23"=>$r_23,"24"=>$r_24);

foreach ($stud as $key => $value) {
    echo $value;
}
?>

Using echo is giving error,
previous issue resolved, expanding my question now. now i want this multi dimensional array to print like below using html tags
#Roll 23#
##Academic##
-Class=>5
-sec=>B
Personal
-Name=>YXZ
-Age=>12
#Roll 24#
##Academic##
-Class=>6
-sec=>A
Personal
-Name=>ABC
-Age=>12

--Nested foreach part with HTML tags--

foreach ($stud as $key => $value) {
    echo "<h1>Roll $key</h1>";
    echo "<ol>";
    foreach ($r_23 as $key => $value) {
    echo "<h2>$key</h2>";
    echo "<ul>";
    foreach ($p_23 as $key => $value){
    echo "<li>$key => $value</li>";}
    echo "</ul>";}
    echo "</ol>";

but it is showing the same value for both academic and personal keys, which i dont exactly want. Thank u!!

Madhu Rima
  • 17
  • 6

2 Answers2

0

$value is an array, echo will only print strings, you need to either JSON encode your $value and echo it or use var_dump. If your intended output was more complex than this then you would need to expand on your question.

Shardj
  • 1,800
  • 2
  • 17
  • 43
-1

Well, its a multi-dimensional array, so you need more foreach()

Here is the updated code:

<?php
$p_23 = array("Name"=>"XYZ","Age"=>"12");
$a_23 = array("Class"=>"5","Sec"=>"A");
$r_23 = array("Personal"=>$p_23,"Academic"=>$a_23);
$p_24 = array("Name"=>"ABC","Age"=>"14");
$a_24 = array("Class"=>"6","Sec"=>"B");
$r_24 = array("Personal"=>$p_24,"Academic"=>$a_24);
$stud = array("23"=>$r_23,"24"=>$r_24);
foreach ($stud as $key => $value) {
    foreach($value as $k => $v){
        foreach($v as $kk => $vv) {
            echo $vv;
        }
    }
}
?>

And this is your multi-dimensional array:

Array
(
    [23] => Array
        (
            [Personal] => Array
                (
                    [Name] => XYZ
                    [Age] => 12
                )

            [Academic] => Array
                (
                    [Class] => 5
                    [Sec] => A
                )

        )

    [24] => Array
        (
            [Personal] => Array
                (
                    [Name] => ABC
                    [Age] => 14
                )

            [Academic] => Array
                (
                    [Class] => 6
                    [Sec] => B
                )

        )

)
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • Now I hv used nested foreach to print the array in an unordered list form, but smhow missing the point exactly. – Madhu Rima Aug 17 '17 at 16:09