0

I want to use array_walk_recursive to extract values of one array and put them into another array. So I did this:

  1. Created a multidimensional test array (where you can find 2 keys named "second_key")
  2. Created a second array and pushed some test entries
  3. 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);
    ?>
8
  • 1
    Perhaps stackoverflow.com/questions/24751543/… helps. Commented Sep 9, 2023 at 15:07
  • @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);. Commented Sep 9, 2023 at 15:14
  • 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... Commented Sep 9, 2023 at 15:44
  • @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). Commented Sep 9, 2023 at 15:47
  • @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? Commented Sep 9, 2023 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.