array_walk()

向函数传递数组值array_walk()
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2
\n"; } echo "Before ...:\n"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk($fruits, 'test_print'); /*Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple*/ function myfunc1tion($value,$key) { echo "The key $key has the value $value
"; } $a=array("a"=>"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunc1tion"); /*The key a has the value red The key b has the value green The key c has the value blue*/ $arr = array( 'num' => array('111','222'), 'ip' => array('ip1','ip2') ); $result = array(); foreach($arr as $key => $value) { $i = 0; foreach($value as $data) { $result[$i][$key] = $data; ++$i; } } $result=json_encode($result); print_r($result); echo "
"; //[{"num":"111","ip":"ip1"},{"num":"222","ip":"ip2"}] function myfunction($value,$key) { // echo "The key $key has the value $value
"; var_dump($value); } $a=array( '0'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue"), '1'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew"), '3'=>array("id"=>"2","a"=>"red","b"=>"green","c"=>"blue"), '4'=>array("id"=>"3","a"=>"back","b"=>"go","c"=>"yew"), '5'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue"), '6'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew") ); array_walk($a,"myfunction"); echo "
"; /*D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '1' (length=1) 'a' => string 'red' (length=3) 'b' => string 'green' (length=5) 'c' => string 'blue' (length=4) D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '1' (length=1) 'a' => string 'back' (length=4) 'b' => string 'go' (length=2) 'c' => string 'yew' (length=3) D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '2' (length=1) 'a' => string 'red' (length=3) 'b' => string 'green' (length=5) 'c' => string 'blue' (length=4) D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '3' (length=1) 'a' => string 'back' (length=4) 'b' => string 'go' (length=2) 'c' => string 'yew' (length=3) D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '1' (length=1) 'a' => string 'red' (length=3) 'b' => string 'green' (length=5) 'c' => string 'blue' (length=4) D:\phpStudy\PHPTutorial\WWW\php\manual\array.php:247: array (size=4) 'id' => string '1' (length=1) 'a' => string 'back' (length=4) 'b' => string 'go' (length=2) 'c' => string 'yew' (length=3)*/ $a=array( '0'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue"), '1'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew"), '3'=>array("id"=>"2","a"=>"red","b"=>"green","c"=>"blue"), '4'=>array("id"=>"3","a"=>"back","b"=>"go","c"=>"yew"), '5'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue"), '6'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew") ); $b=array( '0'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue","d"=>"want"), '1'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew","d"=>"want"), '3'=>array("id"=>"2","a"=>"red","b"=>"green","c"=>"blue","d"=>"want"), '4'=>array("id"=>"3","a"=>"back","b"=>"go","c"=>"yew","d"=>"want"), '5'=>array("id"=>"1","a"=>"red","b"=>"green","c"=>"blue","d"=>"want"), '6'=>array("id"=>"1","a"=>"back","b"=>"go","c"=>"yew","d"=>"want") ); array_walk($a,function(&$value,$key) use($b){ foreach ($b as $val) { if($value['id']==$val['id']){ $value['new'][]=$val['d']; } } }); print_r($a); echo "
"; /*Array ( [0] => Array ( [id] => 1 [a] => red [b] => green [c] => blue [new] => Array ( [0] => want [1] => want [2] => want [3] => want ) ) [1] => Array ( [id] => 1 [a] => back [b] => go [c] => yew [new] => Array ( [0] => want [1] => want [2] => want [3] => want ) ) [3] => Array ( [id] => 2 [a] => red [b] => green [c] => blue [new] => Array ( [0] => want ) ) [4] => Array ( [id] => 3 [a] => back [b] => go [c] => yew [new] => Array ( [0] => want ) ) [5] => Array ( [id] => 1 [a] => red [b] => green [c] => blue [new] => Array ( [0] => want [1] => want [2] => want [3] => want ) ) [6] => Array ( [id] => 1 [a] => back [b] => go [c] => yew [new] => Array ( [0] => want [1] => want [2] => want [3] => want ) ) ) */

你可能感兴趣的:(array)