我想要一个来自多维数组的键值数组
我有一个如下所示的数组结构,
array
{
[0]=>
{
[name] = "maulik";
[roleId] = 34;
}
[1]=>
{
[name] = "ketan";
[roleId] = 12;
}
[2]=>
{
[name] = "nitish";
[roleId] = 40;
}
[3]=>
{
[name] = "hiren";
[roleId] = 24;
}
}
我想要通过单个 php 函数获得所有 roleId 字段的数组。有没有可用的 php 函数或者我必须像下面这样使用?
$roleIds = array();
foreach($users as $user)
{
$roleIds[] = $user['roleId'];
}
针对搜索引擎访问者的解决方案...
function getRoleIdsAsArray($index)
{
return $index['roleId'];
}
$roleIds = array_map("getRoleIdsAsArray" , $users);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这个是适合您的解决方案:
它使用 array_map,从 php 4 开始可用,匿名函数 自 5.2 版本起引入 php
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
你必须这样做。您可以将其变成您自己的功能,尽管我不确定这是否是您正在寻找的。
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.