Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Foreach and format multidimensional array

I have one multidimensional array $result like this

Array(
  [MY GOOGLE] => 

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

On foreach I need to get display like this

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

For now i have this

$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>";
}

Upvotes: 0

Views: 44

Answers (1)

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

You're overthinking this a little:

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>";
}

Upvotes: 1

Related Questions