我有以下数组
Array
(
[0] => Array
(
[program] => 615
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 572
[campus_title_override] => New Jersey - Moorestown
)
)
)
[1] => Array
(
[program] => 313
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 267
[campus_title_override] => Colorado - Denver
)
[1] => Array
(
[campus] => 269
[campus_title_override] => Connecticut - East Windsor
)
[2] => Array
(
[campus] => 277
[campus_title_override] => Illinois - Melrose Park (Chicago Area)
)
[3] => Array
(
[campus] => 279
[campus_title_override] => Indiana - Indianapolis
)
[4] => Array
(
[campus] => 281
[campus_title_override] => Maryland - Columbia
)
[5] => Array
(
[campus] => 570
[campus_title_override] => New Jersey - Mahwah (Bergen County/NY Metro Area)
)
[6] => Array
(
[campus] => 580
[campus_title_override] => New Jersey - Union
)
[7] => Array
(
[campus] => 576
[campus_title_override] => New York - Queens
)
[8] => Array
(
[campus] => 574
[campus_title_override] => Pennsylvania - Philadelphia
)
[9] => Array
(
[campus] => 560
[campus_title_override] => Tennessee - Nashville
)
[10] => Array
(
[campus] => 557
[campus_title_override] => Texas - Grand Prairie (Dallas/Fort Worth Metroplex)
)
)
)
[2] => Array
(
[program] => 617
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 269
[campus_title_override] => Connecticut - East Windsor
)
[1] => Array
(
[campus] => 279
[campus_title_override] => Indiana - Indianapolis
)
[2] => Array
(
[campus] => 570
[campus_title_override] => New Jersey - Mahwah (Bergen County/NY Metro Area)
)
[3] => Array
(
[campus] => 557
[campus_title_override] => Texas - Grand Prairie (Dallas/Fort Worth Metroplex)
)
)
)
[3] => Array
(
[program] => 582
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 267
[campus_title_override] => Colorado - Denver
)
[1] => Array
(
[campus] => 269
[campus_title_override] => Connecticut - East Windsor
)
[2] => Array
(
[campus] => 277
[campus_title_override] => Illinois - Melrose Park (Chicago Area)
)
[3] => Array
(
[campus] => 279
[campus_title_override] => Indiana - Indianapolis
)
[4] => Array
(
[campus] => 560
[campus_title_override] => Tennessee - Nashville
)
[5] => Array
(
[campus] => 557
[campus_title_override] => Texas - Grand Prairie
)
)
)
如果值匹配“New Jersey”,则在campus_title_override
键中我想返回匹配新泽西数组键的数组结果。在当前数组中,array 0, 1, and 2
在"New Jersey"
键中的值为campus_title_override
。我只想返回数组ids 0,1 and 2
。无论与“新泽西”的键值匹配,我想返回主阵列结果集。
我在stackoverflow
中尝试了以下示例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;
}
print_r(search($array, 'campus_title_override', 'New Jersey'));
并返回空结果集。
我也对如何在这个数组上做同样的事情有疑问:
Array
(
[0] => Array
(
[campus] => 269
[campus_title_override] => Connecticut - East Windsor
[programs] => Array
(
[0] => Array
(
[program] => 313
[program_title_override] =>
)
[1] => Array
(
[program] => 582
[program_title_override] =>
)
[2] => Array
(
[program] => 617
[program_title_override] =>
)
[3] => Array
(
[program] => 584
[program_title_override] =>
)
[4] => Array
(
[program] => 619
[program_title_override] =>
)
[5] => Array
(
[program] => 621
[program_title_override] =>
)
[6] => Array
(
[program] => 625
[program_title_override] =>
)
)
)
[1] => Array
(
[campus] => 271
[campus_title_override] => Connecticut - New Britain
[programs] => Array
(
[0] => Array
(
[program] => 619
[program_title_override] =>
)
[1] => Array
(
[program] => 621
[program_title_override] =>
)
[2] => Array
(
[program] => 599
[program_title_override] =>
)
[3] => Array
(
[program] => 607
[program_title_override] =>
)
)
)
[2] => Array
(
[campus] => 273
[campus_title_override] => Connecticut - Shelton
[programs] => Array
(
[0] => Array
(
[program] => 619
[program_title_override] =>
)
[1] => Array
(
[program] => 599
[program_title_override] =>
)
[2] => Array
(
[program] => 607
[program_title_override] =>
)
[3] => Array
(
[program] => 609
[program_title_override] =>
)
)
)
[3] => Array
(
[campus] => 283
[campus_title_override] => Massachusetts - Somerville (Boston Area)
[programs] => Array
(
[0] => Array
(
[program] => 595
[program_title_override] =>
)
[1] => Array
(
[program] => 599
[program_title_override] =>
)
[2] => Array
(
[program] => 605
[program_title_override] =>
)
)
)
[4] => Array
(
[campus] => 564
[campus_title_override] => New Jersey - Iselin (Formerly located in Edison, NJ)
[programs] => Array
(
[0] => Array
(
[program] => 611
[program_title_override] =>
)
[1] => Array
(
[program] => 599
[program_title_override] =>
)
[2] => Array
(
[program] => 607
[program_title_override] =>
)
)
)
[5] => Array
(
[campus] => 570
[campus_title_override] => New Jersey - Mahwah (Bergen County/NY Metro Area)
[programs] => Array
(
[0] => Array
(
[program] => 313
[program_title_override] =>
)
[1] => Array
(
[program] => 617
[program_title_override] =>
)
[2] => Array
(
[program] => 619
[program_title_override] =>
)
[3] => Array
(
[program] => 621
[program_title_override] =>
)
)
)
答案 0 :(得分:1)
对于每个元素循环,通过它campuses
属性并检查是否找到了您想要的条件。
如果满足条件,则将主id插入数组并中断第2个for循环。
示例:强>
$ids = [];
$array = [
[
'campuses' => [
[
'campus_title_override' => 'a'
]
]
]
];
foreach ($array as $id => $obj)
{
foreach ($obj['campuses'] as $campus)
{
if ($campus['campus_title_override'] == 'a')
{
array_push($ids, $id);
break;
}
}
}
var_dump($ids);
您可以详细了解Multi Demensional Arrays here和here。
请阅读文章以了解此代码的作用,并且不要复制它;)
答案 1 :(得分:1)
您可以使用2个foreach
(1个用于元素,1个用于校园)和strpos()
来搜索campus_title_override
的值是否包含搜索值。
$array = array(/*your data*/);
$search = "New Jersey";
$out = [];
// for each element in the array,
foreach ($array as $index => $item) {
// for each campuses,
foreach ($item['campuses'] as $campus) {
// if 'campus_title_override' contains search,
if (strpos($campus['campus_title_override'], $search) !== false) {
$out[] = $item ;
break; // no need to continue, we got the item.
}
}
}
print_r($out);
Array
(
[0] => Array
(
[program] => 615
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 572
[campus_title_override] => New Jersey - Moorestown
)
)
)
[1] => Array
(
[program] => 313
[program_title_override] =>
[campuses] => Array
(
[0] => Array
(
[campus] => 267
[campus_title_override] => Colorado - Denver
)
[1] => Array
(
[campus] => 269
[campus_title_override] => Connecticut - East Windsor
)
[2] => Array
(
[campus] => 277
[campus_title_override] => Illinois - Melrose Park (Chicago Area)
)
[3] => Array
(
[campus] => 279
[campus_title_override] => Indiana - Indianapolis
)
[4] => Array
(
[campus] => 281
[campus_title_override] => Maryland - Columbia
)
[5] => Array
(
[campus] => 570
[campus_title_override] => New Jersey - Mahwah (Bergen County/NY Metro Area)
)
答案 2 :(得分:0)
对于深度为2的数组,即
docker rmi $(docker images | sed -n '1!p' | awk '{print $3}')
您可以使用array_search但它不适用于您的阵列。要搜索,您必须使用foreach循环手动搜索。
array(
[0] => array(
[key] => 'value'
)
)
我希望这会有所帮助。