2012-05-30 07:35

# 在多维数组中查找值

I've a multidimensional array:

``````array (
array (
"favoriteGame"    => "Mario"
)
array (
"favoriteGame"    => "Mario"
)
array (
"favoriteGame"    => "Zelda"
)
)
``````

How could I get the usernames of the persons that like to play for example Mario the easiest way possible?

EDIT: My fault: forget to explicitly mention that the "favoriteGame" value is dynamic and I cannot know which it is in advance.

My Solution:

``````foreach(\$users as \$key => \$value)
{
if(!isset(\$\$value['favoriteGame']))
{
\$\$value['favoriteGame'] = array();
}
}
``````

Iterate over each sub-array and find its favoriteGame value. If there is not already an array \$favoriteGame create it. Push the username-value of the actual sub-array to the \$favoriteGame array.

Thanks for your replies, I just couldn't phrase this question properly.

#### 图片转代码服务由CSDN问答提供 功能建议

``` ``` array（\  n array（
“favoriteGame”=＆gt;“Mario”
）
array（
“favoriteGame”  =＆gt;“Mario”
）

“用户名”=＆gt;“xyz”，
“favoriteGame”=＆gt;“Zelda”
）
）

我怎样才能以最简单的方式获取喜欢玩Mario的人的用户名？

编辑：

我的解决方案：

foreach（\$ users as \$ key =＆gt; \$ value）
{
if if（！isset（\$\$ value ['favoriteGame']））
{
\$  \$ value ['favoriteGame'] = array（）;
}
}

迭代每个su  b-array并找到它最喜欢的游戏值。

感谢您的回复，我无法正确地说出这个问题。
``````
` `
``` 点赞 写回答 关注问题 收藏 分享 邀请回答 ```
``` 7条回答 默认 最新 duanjian3920 2012-05-30 07:45 已采纳 I would use array_filter. If you have PHP 5.3 or up, you can do it like this: \$favorite = "Mario"; \$filter = function(\$player) use(\$favorite) { return \$player['favoriteGame'] == \$favorite; }; \$filtered = array_filter(\$players, \$filter); It will be a little different for older versions because you won't be able to use lambda functions. 点赞 打赏 评论 分享 duanmiao6695 2012-05-30 07:38 function getUsernamesByFavoriteGame(\$data, \$game) { \$usernames = array(); foreach(\$data as \$arr) { if (\$arr['favoriteGame'] == \$game) { \$usernames[] = \$arr['username']; } } return \$usernames; } 点赞 打赏 评论 分享 duanbamo0127 2012-05-30 07:38 \$usernames = array(); foreach(\$array as \$key => \$value) { if (\$value['favoriteGame'] == 'Mario') { \$usernames[] = \$value['username']; } } 点赞 打赏 评论 分享 douyue1926 2012-05-30 07:44 \$game = 'Mario'; \$users = array(); foreach(\$array as \$key => \$value) { if (\$value['favoriteGame'] == \$game) { \$users[] = \$value['username']; } } 点赞 打赏 评论 分享 dongtang7347 2012-05-30 07:44 If you are using this more often then convert the data structure to something like this. array( "Mario" => array( "0":"foo", "1":"xyz" ) "Zelda" => array( "0":"pqr", "1":"abc" ) ) This will directly give you list of user names for a favorite game. \$arr[\$favGame] If you cannot change the data structure then go with with tigrang has suggested. 点赞 打赏 评论 分享 douwu3763 2012-05-30 08:02 I think you should implement a custom multidimensional search function. Take a look at this answer. Here's how you would use it Code | Live example function search(\$array, \$key, \$value){ \$results = array(); if (is_array(\$array)) { if (isset(\$array[\$key]) && \$array[\$key] == \$value) \$results[] = \$array; foreach (\$array as \$subarray) \$results = array_merge(\$results, search(\$subarray, \$key, \$value)); } return \$results; } \$arr = array ( array ( "username" => "foo", "favoriteGame" => "Mario" ), array ( "username" => "bar", "favoriteGame" => "Mario" ), array ( "username" => "xyz", "favoriteGame" => "Zelda" ) ); print_r(search(\$arr, 'favoriteGame', 'Mario')); //OUTPUT Array ( [0] => Array ( [username] => foo [favoriteGame] => Mario ) [1] => Array ( [username] => bar [favoriteGame] => Mario ) ) 点赞 打赏 评论 分享 dongyi9298 2014-07-12 14:04 \$array = array( 'a' => 'A', 'b'=>'B', 'c'=>'C', 'd'=>array( 'e'=>array( 'f'=>'D' ), 'g'=>array( 'h'=>'E' ) ), 'i'=>'F', 'j'=>array( 'k'=>'G' ), 'l'=>'H' ); \$new_array = array(); foreach(\$array as \$k1=>\$v1){ if(is_array(\$v1)){ \$new_array = parseArray(\$new_array, \$k1, \$v1); }else{ \$new_array = array_merge(\$new_array, array(\$k1=>\$v1)); } } function parseArray(\$new_array, \$key, \$val){ if(is_array(\$val)){ foreach(\$val as \$k2=>\$v2){ if(is_array(\$v2)){ \$new_array = parseArray(\$new_array, \$k2, \$v2); }else{ \$new_array = array_merge(\$new_array, array(\$k2=>\$v2)); } } }else{ \$new_array = array_merge(\$new_array, array(\$key=>\$val)); } return \$new_array; } Output Array ( [a] => A [b] => B [c] => C [f] => D [h] => E [i] => F [k] => G [l] => H ) 点赞 打赏 评论 分享 报告相同问题？ 提交 关注问题 相关推荐 更多相似问题 在多维数组中查找值 php 2012-05-30 07:35 回答 7 已采纳 I would use array_filter. If you have PHP 5.3 or up, you can do it like this: \$favorite = "Mario" 在多维数组中查找重复值 php 2016-01-24 19:40 回答 2 已采纳 \$output = array(); // loop through each user foreach (\$users as \$id => \$ips) { // loop thro 在多维数组中查找值 - PHP php 2015-08-11 16:58 回答 1 已采纳 Try this: <?php \$festivals = array ( 1 => array( 'festivalname' => 'Festiva php实现在多维数组中查找特定value的方法 2020-10-23 20:44 主要介绍了php实现在多维数组中查找特定value的方法,实例分析了php实现多维数组的遍历及unset删除的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下 python实现在多维数组中挑选符合条件的全部元素 2020-09-18 09:40 今天小编就为大家分享一篇python实现在多维数组中挑选符合条件的全部元素，具有很好的参考价值，希望对大家有所帮助。一起跟随小编过来看看吧 JS实现遍历不规则多维数组的方法 2020-10-18 15:52 主要介绍了JS实现遍历不规则多维数组的方法,涉及javascript数组递归遍历相关实现与使用技巧,需要的朋友可以参考下在多维数组中查找索引 php 2013-09-16 18:16 回答 3 已采纳 You will want to do a foreach loop for this. But honestly if you structured your array indexes bet 在嵌套的多维数组中查找键值 php 2018-06-11 21:58 回答 2 已采纳 Here's my approach at writing a function to check the keys of your array using the Recursive Itera 循环遍历多维数组并查找匹配值 php 2014-07-31 09:12 回答 1 已采纳 You'll probably want to use 2 loops. One to build the structure and another to do something with i php在多维数组中根据键名快速查询其父键以及父键值的代码 2020-10-28 13:56 有一个多维数组，有多少维大家...假如我们要在这个数组中找一个键为'subIndex'的值，我们可以用for、foreach等方法遍历查找 反过来，假如我们任意给出一个或多个键，要求找出这个键的父级数组的键和值。这又如何实现？ php实现遍历多维数组的方法 2020-10-23 06:40 主要介绍了php实现遍历多维数组的方法,涉及php针对多维数组的遍历与递归操作实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下 JS实现查找数组中对象的属性值是否存在示例 2020-10-16 22:52 主要介绍了JS实现查找数组中对象的属性值是否存在,涉及javascript针对json数组的遍历、查找相关操作技巧,需要的朋友可以参考下在多维数组中查找匹配项 php 2017-11-04 17:26 回答 4 已采纳 The phone number is in the key [0] of each first-level array element, so you can't compare each in 在多维数组中查找子数组 php 2014-06-14 10:55 回答 4 已采纳 \$arr = array('orders' => array ( 0 => Array ( 在多维数组中查找并删除\$ key的所有元素 php 2015-05-09 21:02 回答 2 已采纳 You don't need to pass by reference since you're not trying to change a value in an array. But you python统计多维数组的行数和列数实例 2020-09-20 08:17 今天小编就为大家分享一篇python统计多维数组的行数和列数实例，具有很好的参考价值，希望对大家有所帮助。一起跟随小编过来看看吧 C语言二维数组中的查找的实例 2020-08-29 23:19 主要介绍了C语言二维数组中的查找的实例的相关资料,需要的朋友可以参考下 php查找某个值是否存在于多维数组中 2018-08-15 16:02 可以吧可以吧的博客 * 某个值是否存在于多维数组中 */ public function deep_in_array(\$value, \$array) { \$total =[]; foreach (\$array as \$item) { if (!is_array(\$item)) { if (\$item == \$value) { return \$item; } else { ... PHP在多维会话数组中查找元素值 php 2016-08-08 09:51 回答 2 已采纳 You should start with the beginning: Make it work with a simple foreach (that is the most efficien 多维数组递归查找 2020-08-19 18:16 chen-beibei的博客 let test = [{id:1},{id:5,children:[{id:6,children:[{id:7}]}]}] function chbehavior(arr, id) { let res; (function deepId(arr, id) { for (let a of arr) { if (a.id === id) { res = a;... 没有解决我的问题, 去提问```
``` 点击登录 提问题 ◇ 欢迎建议意见 ◇ 回答问题能得到什么 ◇ 如何写高质量的提问和回答 ◇ 问答平台结题规则说明 加急问题 ¥100 namenode和datanode无法启动解决方案 ¥100 谷歌翻译api有没有破解办法，是不是唯一解决办法就是收费 ¥100 谁知道哪个app上面可以获取自己想要的链接。或者是购买链接 ¥100 求不定方程式整数解问题。 ¥100 yii2的advance版本使用yii2-rest-rbac的示例，认证方式oauth2 ¥100 使用https后，net4.6编译的web应用的oracle连接池异常，不能持续保持连接。但是在普通http协议下，oracle连接池是工作正常的，与oracle服务端的连接能持续。 ¥100 萌新求问会的朋友跑通这两个数据集 ¥100 高通平台camx相机驱动 ¥66 添加内容使得出的非0变量个数最少（即xi尽可能为0）） ¥50 如何设计控制汽车运动的程序 ```
``` ```
``` window.csdn.sideToolbar = { options: { afterFinished: function () { setTimeout(function () { var helphtml = '<a class="option-box" data-type="help" href="https://ask.csdn.net/questions/7441224" target="_blank">'+'<img src="https://g.csdnimg.cn/side-toolbar/2.5/images/bangzhucopy.png" alt="" srcset="">'+ '<span class="show-txt">帮助</span></a>'; \$('.csdn-side-toolbar > a:last').before(helphtml); }, 300); }, contentEl: document.getElementsByClassName('floor')[0] } } ```