Neftali Acosta
Neftali Acosta

Reputation: 418

How to read array multidimensional in php

array(1) {
  ["Tips"] => string(1)
  "1"
}
array(1) {
  ["Noticias"] => string(1)
  "2"
}
array(1) {
  ["Anuncios"] => string(1)
  "3"
}
array(1) {
  ["Consejos"] => string(1)
  "5"
}

I have a array in php and I need to read it for create a list, but I don't know how do it. I need to print it:

<li><a href="cat=1">Tips </a></li>
<li><a href="cat=2">Noticias</a></li>
<li><a href="cat=3">Anuncios</a></li>
<li><a href="cat=5">Consejos</a></li>

Upvotes: 0

Views: 46

Answers (1)

Jamie M
Jamie M

Reputation: 421

Assuming the top level arrays are part of a parent array, then you can do the following:

foreach ($parent_array as $child_array) {
   foreach ($child_array as $key => $value) {
     echo "<li><a href=\"cat={$value}\">{$key}</a></li>";
   }
}

Note that unless you're generating all the output variables yourself in a very controlled manner, you should take careful steps to sanitise the output before echoing it.

Upvotes: 1

Related Questions