I want to use array_walk_recursive to extract values of one array and put them into another array. So I did this:
- 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);
?>
$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...