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:
It use array_map, available since php 4 and anonymous functions introduced into php since version 5.2