Jump to content

Outputting multidimensional array


AdRock

Recommended Posts

I have an array like but larger and it could have different types and levels.  This array is taken from MySQL and I've called it $courses

 


array(23) {
  [0]=>
  array(8) {
    ["name"]=>
    string(43) "Conservation of Decorative Arts"
    ["type"]=>
    string(12) "conservation"
    ["url"]=>
    NULL
    ["level"]=>
    string(1) "1"
  }
  [1]=>
  array(8) {
    ["name"]=>
    string(32) "Diploma in Restoration"
    ["type"]=>
    string(12) "conservation"
    ["url"]=>
    NULL
    ["level"]=>
    string(1) "1"
  }
  [2]=>
  array(8) {
    ["name"]=>
    string(20) "Diploma in Finishing"
    ["type"]=>
    string(12) "craft"
    ["url"]=>
    NULL
    ["level"]=>
    string(1) "1"
}

What I want to do is output the array so i can echo out the Name and URL and order it by Type so i get something like

<h1>Conservation</h1>
<ul>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>
<h1>Craft</h1>
<ul>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>

I 've tried using nested foreach() loops but it prints the same value out multiple times so instead of about 15 rows, i end up with about 100 with loads of duplicate rows one after the other

 

// $this->courses is the MySQL array
<?php foreach($this->courses as $course): ?>
<?php foreach($course as $key => $value): ?>

<li><?=$course['name'];?></li>

<?php endforeach; ?>
<?php endforeach; ?>

What am i doing wrong?

 

Link to comment
https://forums.phpfreaks.com/topic/284846-outputting-multidimensional-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.