0
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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.