循环遍历$arrayA的所有数组
foreach($arrayA as $array)
{
// Check each array has level value 2 or not
if ($array['level'] == 2)
{
// found value
echo "found the array";
}
}
要获取具有特定级别的数组
2
,您可以使用array_filter(),例如,
$result = array_filter($arrayA, function($k) {
return $k['level'] == 2;
});
print_r($result);
和使其动态化,要获得
parent_ohp_id
为空白的最低级别,然后使用,
$result = array_filter($arrayA, function($k) {
return $k['parent_ohp_id'] == "";// this is the root level because it has no parent id
});
print_r($result);
一种方法是使用
usort()
函数对 level
键上的表格进行排序,然后获取数组的第一个元素:
<?php
$array = [
[
'john' => 'Snow',
'level' => 5,
],
[
'john' => 'Cena',
'level' => 8,
],
[
'john' => 'Kennedy',
'level' => 2,
],
[
'john' => 'Glenn',
'level' => 12,
],
];
usort($array, function ($a, $b) {
return $a['level'] - $b['level'];
});
echo current($array)['john']; // This will display "Kennedy".
另一种方法是使用
foreach
循环并将级别值与之前的迭代进行比较:
$lowest = $array[0];
foreach ($array as $item) {
if ($item['level'] < $lowest['level']) {
$lowest = $item;
}
}
echo $lowest['john']; // This will also display "Kennedy".