1
oott123 2014-04-30 11:18:03 +08:00 via Android
你在回调里这样:
unset($arr[$b]); $arr[new] = $a; |
3
bingu 2014-04-30 14:28:34 +08:00
手册的array_walk范例差不多就符合你的要求了吧。
Example #1 array_walk() 例子 <?php $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<br />\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 |
5
yakczh OP @oott123
array_walk($test_array, function($a, $b) use (&$test_array) { unset($test_array[$b]); $test_array['http_'.$b]=$a; }); 出来是 Array ( [connection] => close [http_http_version] => 1.1 ) 如果是 array_walk($test_array, function(&$a, $b) use (&$test_array) { //unset($test_array[$b]); $test_array['http_'.$b]=$a; }); 则没反应了 |
6
icanc 2014-04-30 15:10:03 +08:00
$test_array = call_user_func(
function (Closure $callback,$array){ $results = array(); foreach ($array as $key => $value){ list($innerKey, $innerValue) = call_user_func($callback, $key, $value); $results[$innerKey] = $innerValue; } return $results; }, function($k,$v){ return array('http_'.$k,$v);}, $test_array // 原始数组 ); |
7
manhere 2014-04-30 21:12:28 +08:00 via Android
批量的添加 和 不添加 有什么区别?
|
8
pubby 2014-05-01 23:16:36 +08:00
$test_array = array_combine(
array_map(function($k){ return 'http_'.$k;},array_keys($test_array)), array_values($test_array) ); var_dump($test_array); |