Jump to content

Outputting multidimensional array


Go to solution Solved by 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.