2012-02-15 80 views
0

對我有這樣的使用array_walk多維數組

Array ([0] => Array ([keyword] => nothing [id] => 44) [1] => Array ([keyword] => everything [id] => 45) [2] => Array ([keyword] => dogs [id] => 46)) 

數組我基本上要創建這樣一個鏈接:

<a href="mypage.php?pid=ID">KEYWORD</a> 

所以我想使用array_walk,但我不認爲這將工作在一個多維數組上,我該如何着手讓它遍歷我的數組並輸出數組中每個條目的ID和關鍵字?

回答

1

使用foreach循環:

$array = array(
    array(
     "keyword" => "anything", 
     "id"  => 44 
    ), 
    array(
     "keyword" => "everything", 
     "id"  => 45 
    ), 
    array(
     "keyword" => "dogs", 
     "id"  => 46 
    ), 
); 

foreach ($array as $link) { 
    $keyword = $link["keyword"]; 
    $id  = $link["id"]; 
    echo "<a href='mypage.php?pid=$id'>$keyword</a>" . PHP_EOL; 
}