如何搜索多维数组以返回多个键 [英] How to search a multidimensional array to return multiple keys
问题描述
我有一个用php编写的多维数组,其中包含一个数组数组。我已经阅读了很多有关如何搜索此内容的信息,但似乎大多数解决方案还是:
I've got a multidimensional array written in php that holds an array of arrays. I've read a lot about how to search this, but it seems most solutions either:
A。要求您具有唯一的键值,例如产品ID
A. require you have unique values for the keys, such as a product id
或
B。我对在数组中返回多个结果感到满意
B. are satisfied with returning multiple results in an array
我要搜索的数组给出了舍入编号(这是数组的数组编号最高/第一级数组)和玩家名称(将是关键玩家1或玩家2的值)。
I am looking to search the array given the round number (which is the array number of the highest/first level array), and a player name (which will be the value of either the key player 1 or player 2).
该数组如下所示:
Array (
[0] => Array ( )
[8] => Array (
[1] => Array (
[Match] => 1
[Player1seed] => (Q)
[Player1name] => Mahut
[Player2seed] => (2)
[Player2name] => Goffin
[Matchscore] => 7-6(1), 6-1
[Round] => Finals
)
)
[7] => Array (
[1] => Array (
[Match] => 1
[Player1seed] => (2)
[Player1name] => Goffin
[Player2seed] =>
[Player2name] => Muller
[Matchscore] => 7-6(4), 6-4
[Round] => Semi-Finals
)
[2] => Array
(
[Match] => 2
[Player1seed] => (Q)
[Player1name] => Mahut
[Player2seed] => (WC)
[Player2name] => Haase
[Matchscore] => 5-7, 6-3, 6-4
[Round] => Semi-Finals
)
)
等。
本质上,我需要能够专门搜索一个子集,例如 array [7] ,并返回包含玩家1或玩家2作为名称的结果,例如高芬。
Essentially, I need to be able to search specifically one subset such as array[7] and be returned the results that contains either player1 or player2 as a name, say Goffin.
但我不希望它返回其他锦标赛回合的结果,例如 array [8] 或 array [ 6] 其中一位是高芬。
But I don't want it to return results from other tournament rounds such as array[8] or array[6] where either player is Goffin.
我似乎在任何地方都找不到这种解决方案。我设置阵列不正确吗?还是希望从较少的数据集中获得数据库功能?
I can't seem to find this solution anywhere. Am I setting up my array incorrectly? Or expecting database functions from a lesser data set?
任何帮助将不胜感激。
推荐答案
这并不是我想要解决的问题的确切方法,但是在标识了特定的轮数之后,我可以通过运行循环来获得想要的结果:
It's not exactly the way I wanted to solve the problem, but I was able to get the results I wanted by running a loop after identifying the specific round number:
$r = $roundnumber;
foreach( $matchesarray[$r] AS $key=>$data ){
$winnerseed=$data['Player1seed'];
$winnername=$data['Player1name'];
$loserseed=$data['Player2seed'];
$losername=$data['Player2name'];
$matchurl=$data['Matchurl'];
$score=$data['Matchscore'];
if ($p1name == $winnername || $p2name == $losername){
$winner=$p1;
}
else if ($p2name == $winnername || $p1name == $losername){
$winner=$p2;
}
}
这篇关于如何搜索多维数组以返回多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!