1

i have a multi-dimensional array like this:

Array
(
[5432980] => Array
    (
        [0] => 223
        [entry_id] => 223
        [1] => 138
        [elo_score] => 138
        [2] => 38
        [hits] => 38
    )

[2712949] => Array
    (
        [0] => 701
        [entry_id] => 701
        [1] => 128
        [elo_score] => 128
        [2] => 28
        [hits] => 28
    )
)

i get the array from here $leader_array = fns_elo_search_entries();

i an trying to get each array ([5432980], [2712949]) in a li using foreach

something like this

<ul>
<li>load here the information from `[5432980]`</li>
<li>load here the information from `[2712949]`</li>
</ul>

i know that i need to use two foreach statements because is an multi-dimensional array any ideas? thanks

2
  • 1
    Why do you need two foreach? Once inside the first, you could access 5432980 keys directly. Apr 25, 2011 at 16:24
  • The mix of integer keys and string keys is a bit funny, what is going on? Are the keys relevant or not?
    – Mog
    Apr 25, 2011 at 16:29

4 Answers 4

3
echo '<ul>';
foreach($array as $valueArray)
{
   echo '<li>';
   foreach($valueArray as $value)
   {
     echo $value;
   }
   echo '</li>';
}
echo '</ul>';
2

Alternate way, comma separated:

foreach ($arr as $key => $values):

    echo "<li>{$key}: ".implode(', ', $values)."</li>";

endforeach;

load here the information from [5432980] is not too clear as far as what you want and how you want it formatted, so I simply gave an alternative to 2 foreach loops.

As mentioned in the comments, you can just access the keys directly.

foreach ($arr as $key => $values):

    echo "<li>Score: ".$values['elo_score']."</li>";//etc.

endforeach;
2
  • i get the array from here $leader_array = fns_elo_search_entries(); Apr 25, 2011 at 16:30
  • and. how do i grab each individual value, if i want to display it? Apr 25, 2011 at 16:33
1
<ul>
<?php
foreach($arrs as $arr){
  echo "<li>";
  foreach($arr as $a){
    echo "value: " . $a;
  }
 echo "</li>";
}

?>

</ul>

This is just basic formatting.

0
1
echo "<ul>";
array_walk($arr, 'ulli');
echo "</ul>";
function ulli(&$value, $index){
    if(is_array($value)){
        echo "<ul>";
        array_walk($value, 'ulli');
        echo "</ul>";
    }else{
        echo '<li>'.$value.'</li>';
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.