array_map() 函數將陣列的每個值傳送到使用者編寫的函數,該函數傳回新值。
array_map(callback, arr1, <strong>arr2 −</strong>, <strong>arr3 −</strong>, <strong>arr4 −</strong>, …)
#callback−回呼函數
<?php function square($n) { return($n * $n); } $arr = array(1, 2, 3); $res = array_map("square", $arr); print_r($res); ?>
Array ( [0] => 1 [1] => 4 [2] => 9 )
<?php $arr1 = array(1, 2, 3); $arr2 = array("steve", "david", "nadal"); $arr3 = array("cricket", "football", "tennis"); $res = array_map(null, $arr1, $arr2, $arr3); print_r($res); ?>
Array ( [0] => Array ( [0] => 1 [1] => steve [2] => cricket ) [1] => Array ( [0] => 2 [1] => david [2] => football ) [2] => Array ( [0] => 3 [1] => nadal [2] => tennis ) )
以上是PHP中的array_map()函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!