gpt4 book ai didi

php issue with array push inside of a function, // reference(函数内数组推送的PHP问题,//Reference)

转载 作者:bug小助手 更新时间:2023-10-28 20:56:24 25 4
gpt4 key购买 nike



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来提取一个数组的值,并将它们放到另一个数组中。所以我这么做了:



  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);
?>

更多回答

Perhaps stackoverflow.com/questions/24751543/… helps.

也许Stackoverflow.com/Questions/24751543/…有帮助。

@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抱歉,抱歉,抱歉--也许我有一点……,但我不明白你的意思:-(我到底要在哪里加一个分号?

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com