Foreach和格式化多维数组

时间:2015-12-05 21:57:43

标签: php foreach

我有一个多维数组 $ result 就像这样

Array(
  [MY GOOGLE] => 

  Array(
    [link] => Google
    [href] => google.com )
)

关于foreach,我需要像这样显示

<p>MY GOOGLE</p>
<a href="google.com">Google</a>

现在我有了这个

$keys = array_keys($result);
    for($i = 0; $i < count($result); $i++) {
    echo $keys[$i] . "<br>";
    foreach($result[$keys[$i]] as $key => $value) {
        echo $value ."<br>";
    }
    echo "<br>";
}

1 个答案:

答案 0 :(得分:1)

你有点过分思考:

foreach($result as $key => $value) {  //Loop through your outer array
    //The key is your 'title'
    echo "<p>".$key."</p>";
    //The href and link inner array values can be filled into the anchor tag.
    echo "<a href='".$value['href']."'>".$value['link']."</a>";
}