array_map inline anonymous function [duplicate]
Asked Answered
S

4

68

I tested inline anonymous function with array_map here

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

$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"

here is the test link showing error

Socorrosocotra answered 1/8, 2013 at 14:21 Comment(4)
Your definition of the $user_meta array is not valid for a PHP array, it's print_r() output, formatted for human readabilityTomi
P.S. inline anonymous functions only work in PHP 5.3+Jardine
@Paulpro why did you rollback the format change?Fowlkes
problem in in the email! need to put that in quotes "[email protected]"Lifeblood
C
113

I hope this will help:

$user_meta = array_map(function ($a) { return $a[0]; }, $user_meta);
Caudillo answered 17/3, 2017 at 13:42 Comment(4)
This is a good answer, except the remark there must be a space between the keyword function and its parameters. Because i removed the space and got no problem.Bay
The space after function keyword really doesn't matter...!Koerlin
The space changes nothing.Lifeblood
Yes, the syntax seems fine. There error should be from $user_meta definition.Caudillo
Y
18

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

Yearlong answered 1/8, 2013 at 14:25 Comment(6)
I am getting same error when I pass the array with arguments..I could only make it work by calling function explicitly.. hereSocorrosocotra
Which version of PHP do you have? Most people have at least 5.3 now, and you're code worked fine in 5.3 and up.Yearlong
I am using php 5.5 only you can check the updated link..again it is showing errorsSocorrosocotra
@BLPraveen There are no errors in the section titled: Output for 5.3.0 - 5.5.1. Only in the older version sections.Yearlong
@BLPraveen Added some output so you can see that's it's working: 3v4l.org/5NgCcYearlong
The array syntax is fine... array('0' => '') is just going to define an associative array with a key '0' pointing to an empty string. The error's almost certainly just that he's on a PHP version older than 5.3.Reasonable
B
10

Slightly shorter could be

$user_meta = array_map(fn ($a) => $a[0], $user_meta);

But I would prefer the array_column approach for such an array_map

Blackstone answered 10/1, 2022 at 2:6 Comment(0)
B
3

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.

$user_meta = array_column($user_meta, 0);
Bowerbird answered 19/12, 2018 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.