[英]PHP check if array of arrays has searching value of key in atleast one of them
我有一個數組,其中包含每天的游戲數據:
"days" => [
"2019-07-31" => [
"3" => [
'id' => 3,
'type' => normal,
'teams' => [
'teamA', 'teamB',
],
'winnerTeam' => 'teamB',
],
"4" => [
'id' => 4,
'type' => ranked,
'teams' => [
'teamC', 'teamD',
],
'winnerTeam' => 'teamC',
],
],
"2019-07-30" => [
"1" => [
'id' => 1,
'type' => normal,
'teams' => [
'teamA', 'teamC',
],
'winnerTeam' => 'teamA',
],
"2" => [
'id' => 2,
'type' => normal,
'teams' => [
'teamB', 'teamD',
],
'winnerTeam' => 'teamC',
],
],
];
我想獲取有關所有排名游戲的數據,但在此之前,我想檢查一下是否有排名游戲發生。 有人比說說一切都知道更優雅的方法嗎? 所以我的目標是在獲取數據之前檢查是否有任何game['type'] === 'ranked'
您可以使用array-column和array-count-values來實現。
對於每一天,使用array_column
獲取所有類型,然后對其計數並返回,如果在返回數組中設置了“ ranked”類型。
考慮以下:
foreach($arr["days"] as $day => $a) {
$aaa = array_count_values(array_column($a, "type"));
if (isset($aaa['ranked']))
echo "Found in $day \n";
}
直播示例: 3v4l
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.