I have an array structure like below
array
{
[0]=>
{
[name] = "maulik";
[roleId] = 34;
}
[1]=>
{
[name] = "ketan";
[roleId] = 12;
}
[2]=>
{
[name] = "nitish";
[roleId] = 40;
}
[3]=>
{
[name] = "hiren";
[roleId] = 24;
}
}
I want array of all roleId field by a single php function. Is there any php function available or I have to use like below?
$roleIds = array();
foreach($users as $user)
{
$roleIds[] = $user['roleId'];
}
Solution for the visitors from search engine....
function getRoleIdsAsArray($index)
{
return $index['roleId'];
}
$roleIds = array_map("getRoleIdsAsArray" , $users);
maybe this one is a solution suitable to you:
$roleIds = array_map(function($c) {return $c['roleId'];}, $users);
It use array_map, available since php 4 and anonymous functions introduced into php since version 5.2
You have to do it like that. You can turn that into your own function, though I'm not sure if that's what you're looking for.