比较foreach循环中的子数组
Compare sub-arrays in foreach loop
我有这个数组,其中包含代表不同条目的子数组:
$array = [
[
["Name" => "Auto", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 7, "SubLangId" => 1]],
["Name" => "自動車", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 17, "SubLangId" => 1]],
["Name" => "car", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 9, "SubLangId" => 1]]
],
[
["Name" => "Antrieb", "EntryId" => 5678, "LanguageId" => ["PrimaryLangId" => 7, "SubLangId" => 1]],
["Name" => "drive", "EntryId" => 5678, "LanguageId" => ["PrimaryLangId" => 9, "SubLangId" => 1]]
]];
我需要检索每个“名称”键的值,但前提是键 PrimaryLangId = 7 并且没有其他键为 PrimaryLangId 的条目= 17。所以在上面的例子中我只想得到“Antrieb”,但我只能让我的脚本得到“Auto”和“Antrieb”。
这是我尝试过的:
foreach ($array as $a) {
for ($i = 0; $i < count($a); $i++) {
if (!in_array(17, $a[$i]['LanguageId']) && $a[$i]['LanguageId']['PrimaryLangId'] == 7) {
print $a[$i]['Name'] . "\n";
}
}}
我得到的输出是:
Auto
Antrieb
我明白为什么我同时得到“Auto”和“Antrieb”,因为我的 if 语句只检查子数组内部,而在这个数组中显然没有 PrimaryLangId = 17 .我缺乏与其他子阵列的比较。我该如何实现?
编辑:如果数组不包含 PrimaryLangId = 17 的子数组,我正在寻找一种只获取子数组名称的方法。所以基本上我需要将子数组相互比较.
在我的例子中,我想要得到输出:
Auto
foreach ($array as $subArray) {
$hasNoPrimaryLangId17 = true;
for ($i = 0; $i < count($subArray); $i++) {
if ($subArray[$i]['LanguageId']['PrimaryLangId'] == 17) {
$hasNoPrimaryLangId17 = false;
}
}
if ($hasNoPrimaryLangId17) {
for ($i = 0; $i < count($subArray); $i++) {
if ($subArray[$i]['LanguageId']['PrimaryLangId'] == 7) {
print $subArray[$i]['Name'] . "\n";
}
}
}
}
我有这个数组,其中包含代表不同条目的子数组:
$array = [
[
["Name" => "Auto", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 7, "SubLangId" => 1]],
["Name" => "自動車", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 17, "SubLangId" => 1]],
["Name" => "car", "EntryId" => 1234, "LanguageId" => ["PrimaryLangId" => 9, "SubLangId" => 1]]
],
[
["Name" => "Antrieb", "EntryId" => 5678, "LanguageId" => ["PrimaryLangId" => 7, "SubLangId" => 1]],
["Name" => "drive", "EntryId" => 5678, "LanguageId" => ["PrimaryLangId" => 9, "SubLangId" => 1]]
]];
我需要检索每个“名称”键的值,但前提是键 PrimaryLangId = 7 并且没有其他键为 PrimaryLangId 的条目= 17。所以在上面的例子中我只想得到“Antrieb”,但我只能让我的脚本得到“Auto”和“Antrieb”。
这是我尝试过的:
foreach ($array as $a) {
for ($i = 0; $i < count($a); $i++) {
if (!in_array(17, $a[$i]['LanguageId']) && $a[$i]['LanguageId']['PrimaryLangId'] == 7) {
print $a[$i]['Name'] . "\n";
}
}}
我得到的输出是:
Auto
Antrieb
我明白为什么我同时得到“Auto”和“Antrieb”,因为我的 if 语句只检查子数组内部,而在这个数组中显然没有 PrimaryLangId = 17 .我缺乏与其他子阵列的比较。我该如何实现?
编辑:如果数组不包含 PrimaryLangId = 17 的子数组,我正在寻找一种只获取子数组名称的方法。所以基本上我需要将子数组相互比较.
在我的例子中,我想要得到输出:
Auto
foreach ($array as $subArray) {
$hasNoPrimaryLangId17 = true;
for ($i = 0; $i < count($subArray); $i++) {
if ($subArray[$i]['LanguageId']['PrimaryLangId'] == 17) {
$hasNoPrimaryLangId17 = false;
}
}
if ($hasNoPrimaryLangId17) {
for ($i = 0; $i < count($subArray); $i++) {
if ($subArray[$i]['LanguageId']['PrimaryLangId'] == 7) {
print $subArray[$i]['Name'] . "\n";
}
}
}
}