php array_map 内联匿名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17996690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:46:39  来源:igfitidea点击:

array_map inline anonymous function

phparray-map

提问by B L Praveen

I tested inline anonymous function with array_maphere

在这里测试了内联匿名函数array_map

and it worked but when I tried same with $user_metait is not working.

它起作用了,但是当我用$user_meta尝试相同时,它不起作用。

$user_meta = Array ( [interest] => Array ( [0] => Array ) [type] => 
     Array ( [0] => Array ) [user_status] => Array ( [0] => deny)
     [firstname] => Array ( [0] => ) [lastname] => Array ( [0] => B ) 
     [email] => [email protected] ) 

$user_meta = array_map(function($a) { return $a[0]; },$user_meta);

"Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in"

“解析错误:语法错误,意外的 T_FUNCTION,需要 ')'”

here is the test link showing error

这是显示错误的测试链接

回答by Dat TT

I hope this will help:

我希望这个能帮上忙:

$user_meta = array_map(function ($a) { return $a[0]; }, $user_meta);

回答by Paul

There's nothing wrong with the array_mapline, but everything before it is wrong. That is the output of a print_rnot PHP code. Compare how you define the array in the two links you posted.

array_map条线没有任何问题,但它之前的一切都是错误的。那是非print_rPHP 代码的输出。比较您在发布的两个链接中定义数组的方式。

回答by Hichem Benali

That's not an answer to your question, but since you want to return the first key of each sub-array, you can just use array_column.

这不是您的问题的答案,但由于您想返回每个子数组的第一个键,您可以使用array_column.

$user_meta = array_column($user_meta, 0);