Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show multidimensional array value in ul li in php

I am getting an array through an API I want to show the values in an unordered list. I’m able to access the values individually but unable to create a loop so all the values can show in a list format.

This is how the array looks

    Array
(
    [0] => Array
        (
            [students] => Array
                (
                    [0] => Array
                        (
                            [name] => Sam
                            [marks] => 75
                        )

                    [1] => Array
                        (
                            [name] => Roy
                            [marks] => 60
                        )

                    [2] => Array
                        (
                            [name] => Josh
                            [marks] => 85
                        )

                    [3] => Array
                        (
                            [name] => Ryan
                            [marks] => 45
                        )

                    [4] => Array
                        (
                            [name] => Dev
                            [marks] => 68 
                        )

                )

        )

)

I want to create a list like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<ul>
<li>Sam 75</li>
<li>Roy 60</li>
<li>Josh 85</li>
<li>Ryan 45</li>
<li>Dev 68</li>
</ul>

How can I create a loop to show the values like this in PHP. Any help or suggestion would be very helpful.

>Solution :

<?php

$data = [['students' => [ ... ]]];
$students = $data[0]['students'];

echo "<ul>";
foreach ($students as $student) {
    echo "<li>{$student['name']} {$student['marks']}</li>";
}
echo "</ul>";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading