I want to use array_walk_recursive to extract values of one array and put them into another array.
So I did this:
我想使用ARRAY_WALK_RECURSIVE来提取一个数组的值,并将它们放到另一个数组中。所以我这么做了:
- Created a multidimensional test array (where you can find 2 keys named "second_key")
- Created a second array and pushed some test entries
- Now I want to extract all values of "second_key" and push to the new array But I am facing the issue that it will not work, most likely because of missing reference.
Any idea how to fix this?
<?php
$test_array = array();
$test_array['NUMERO_ONE'] = array(
"initial_key" => "Alpha",
"details" => array(
"first_key" => ".Alpha_1",
"second_key" => "Alpha_2"
)
);
$test_array['NUMERO_TWO'] = array(
"initial_key" => "Beta",
"details" => array(
"first_key" => ".Beta_1",
"second_key" => "Beta_2"
)
);
var_dump($test_array);
echo "<br>***********************************<br>";
$results_array = array();
$results_array[] = "banana";
array_push($results_array, "tomato");
var_dump($results_array);
echo "<br>***********************************<br>";
// ?? $results_array should be a reference, but how??
// see here: https://stackoverflow.com/questions/20169327/array-push-not-working-within-function-php
function my_function($value, $key, &$results_array) {
echo "Content of $key is: $value <br>";
if ($key == "second_key") {
echo "SECOND KEY: " . $value . "<br>";
$results_array[] = $value;
// even the next simple test is not working
$results_array[] = "test_A";
// also array_push is not working
array_push( $results_array, $value);
}
}
array_walk_recursive($test_array,"my_function", $results_array);
echo "<br>***********************************<br>";
var_dump($results_array);
?>
更多回答
@NigelRen is correct. Define your function as a closure: $my_function = static function ($value, $key) use (&$results_array) { ... } and use it like this: array_walk_recursive($test_array, $my_function, $results_array);.
@NigelRen是正确的。将函数定义为闭包:$MY_Function=Static Function($VALUE,$KEY)USE(&$RESULTS_ARRAY){...}并按如下方式使用:ARRAY_WAK_RECURSIVE($TEST_ARRAY,$MY_Function,$RESULTS_ARRAY);。
Thanks, I immediately tried: $my_function = static function ($value, $key) use (&$results_array) { var_dump($results_array); } array_walk_recursive($test_array, $my_function, $results_array);
...but no success either... Is there a way to put the reference of $results_array to array_walk_recursive() so it can be passed? Trying now answer 4 of the linked question...
谢谢,我立即尝试了:$MY_Function=静态函数($VALUE,$KEY)USE(&$RESULTS_ARRAY){VAR_DUMP($RESULTS_ARRAY);}ARRAY_WAK_RECURSIVE($TEST_ARRAY,$MY_Function,$RESULTS_ARRAY);...但也没有成功...有没有办法将$RESULTS_ARRAY的引用放到ARRAY_WALK_RECURSIVE()中,以便可以传递?现在正在尝试回答链接问题的第四个问题...
@jochenthomas The code you just posted works, I copy-pasted it and tried it (but note that you are missing a semicolon after the closing brace of the function definition).
@jochenthoma您刚刚发布的代码工作正常,我复制-粘贴并尝试了它(但请注意,您在函数定义的右括号后遗漏了分号)。
@lukas.j sorry, sorry, sorry - maybe I am a little bit ...., but I did not get your point :-( Where exactly I have to add a semicolon?
@lukas.j抱歉,抱歉,抱歉--也许我有一点……,但我不明白你的意思:-(我到底要在哪里加一个分号?
我是一名优秀的程序员,十分优秀!