PHP根据多维数组中的值选择数组
我有一个具有以下结构的数组,我需要从已选择=>1的子数组中选择最大的教育级别,[键]越大,教育级别越高。 如何使用PHP内置的数组函数实现这一点PHP根据多维数组中的值选择数组,php,arrays,select,multidimensional-array,Php,Arrays,Select,Multidimensional Array,我有一个具有以下结构的数组,我需要从已选择=>1的子数组中选择最大的教育级别,[键]越大,教育级别越高。 如何使用PHP内置的数组函数实现这一点 Array ( [0] => Array ( [key] => 0
Array
(
[0] => Array
(
[key] => 0
[selected] => 1
[value] => Highschool diploma
)
[1] => Array
(
[key] => 1
[selected] => 0
[value] => Vocational training
)
[2] => Array
(
[key] => 2
[selected] => 0
[value] => College degree (Outside Quebec)
)
[3] => Array
(
[key] => 3
[selected] => 1
[value] => College degree (Quebec)
)
[4] => Array
(
[key] => 4
[selected] => 1
[value] => Baccalaureate
)
[5] => Array
(
[key] => 5
[selected] => 0
[value] => Masters degree
)
[6] => Array
(
[key] => 6
[selected] => 0
[value] => Doctorate
)
)
当然。循环遍历每个内部数组,并对照当前位于顶部的数组检查它们的值。例如:
PHP>=5.5.0 要获取所有选定的关键点,请执行以下操作:
$keys = array_filter(array_column($array, 'selected'));
// or if there can be values other than 0 and 1
$keys = array_keys(array_column($array, 'selected'), '1');
要获取具有最高值的密钥,请执行以下操作:
$max = max(array_filter(array_column($array, 'selected')));
// or if there can be values other than 0 and 1
$max = max(array_keys(array_column($array, 'selected'), '1'));
array\u walk($data,function($el)use(&$ret){
//或者如果(空($ret)。。。
如果(!isset($ret)| |($el['selected']>=1&&$ret['key']<$el['key']))
$ret=$el;
});
var_dump($ret);
只是不要忘记取消设置或设置$ret=false;//null等…
如果您想多次运行此代码:)如果您想使用php内置程序,这可能是最好的选择。像这样的事情应该可以做到:
$result = array_reduce($theArray, function($state, $item) {
if($item['selected'] !== 1) return $state;
if($state === null) return $item;
if($item['key'] > $state['key']) return $item;
return $state;
});
echo $result['value'];
更新:我应该注意,上面的内容只适用于PHP5.3或更高版本,因为它使用的是早期版本的PHP中不可用的。如果您使用的是早期版本,那么您确实应该升级。但如果无法升级,则必须将函数部分定义为普通的独立函数,然后在第二个参数中将函数名(作为字符串)传递给
array\u reduce
。此方法在文档页面上的示例中显示为array\u reduce
我已经为您构建了一个测试函数。
测试和工作!为毕业干杯
<?php
// Demo Data
$your_array = array(
array(
'key'=>0,
'selected'=>1,
'value'=>'Highschool diploma'
),
array(
'key'=>1,
'selected'=>0,
'value'=>'Vocational training'
),
array(
'key'=>2,
'selected'=>0,
'value'=>'College degree (Outside Quebec)'
),
array(
'key'=>3,
'selected'=>1,
'value'=>'College degree (Quebec)'
),
array(
'key'=>4,
'selected'=>1,
'value'=>'Baccalaureate'
),
array(
'key'=>5,
'selected'=>0,
'value'=>'Masters degree'
),
array(
'key'=>6,
'selected'=>0,
'value'=>'Doctorate'
)
);
// Actual function
$array_count = (count($your_array)-1);
$highest_education = 'Nothing found.';
for($i=$array_count;$i>0;$i--)
{
if($your_array[$i]['selected']==1)
{
$highest_education = $your_array[$i]['value'];
break;
}
}
// Testing output
echo $highest_education;
?>
$result = array_reduce($theArray, function($state, $item) {
if($item['selected'] !== 1) return $state;
if($state === null) return $item;
if($item['key'] > $state['key']) return $item;
return $state;
});
echo $result['value'];
<?php
// Demo Data
$your_array = array(
array(
'key'=>0,
'selected'=>1,
'value'=>'Highschool diploma'
),
array(
'key'=>1,
'selected'=>0,
'value'=>'Vocational training'
),
array(
'key'=>2,
'selected'=>0,
'value'=>'College degree (Outside Quebec)'
),
array(
'key'=>3,
'selected'=>1,
'value'=>'College degree (Quebec)'
),
array(
'key'=>4,
'selected'=>1,
'value'=>'Baccalaureate'
),
array(
'key'=>5,
'selected'=>0,
'value'=>'Masters degree'
),
array(
'key'=>6,
'selected'=>0,
'value'=>'Doctorate'
)
);
// Actual function
$array_count = (count($your_array)-1);
$highest_education = 'Nothing found.';
for($i=$array_count;$i>0;$i--)
{
if($your_array[$i]['selected']==1)
{
$highest_education = $your_array[$i]['value'];
break;
}
}
// Testing output
echo $highest_education;
?>