Reputation: 1495
I have an array with the below structure and I need to select the greatest education level from the sub arrays that have selected=>1, the greater the [key] the greater the education level. anyway to do this with PHP built in array functions?
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
)
)
Upvotes: 5
Views: 23489
Reputation: 13542
If you want to use php built-ins, array_reduce
is probably the way to go. Something like this should do the trick:
$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'];
update: I should note that the above only works in PHP 5.3 or later, because it uses anonymous functions, which weren't available in earlier versions of PHP. If you're working with an earlier version, you really should upgrade. But if you can't upgrade, then you'll have to define the function part as a normal stand-alone function, and then pass the name of your function (as a string) in the second argument to array_reduce
. This approach is shown in the examples on the doc page for array_reduce
.
Upvotes: 1
Reputation: 16214
array_walk($data, function($el) use(&$ret) {
// or if (empty($ret) ...
if (!isset($ret) || ($el['selected'] >= 1 && $ret['key'] < $el['key']))
$ret = $el;
});
var_dump($ret);
Just do not forget to unset or set $ret = false; //null, etc..
if you want to run this code multiple times :)
Upvotes: 1
Reputation: 1035
I've build a test function for you. Tested and working! Cheers to Baccalaureate!
<?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;
?>
Upvotes: 0
Reputation: 78994
PHP >= 5.5.0
To get all the selected keys:
$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');
To get the key with the highest value:
$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'));
Upvotes: 2
Reputation: 11375
Sure. Loop through each inner array, and check their values against the one that is currently top. For example: https://eval.in/private/5c5a2ba8015119
$final = array();
foreach($array as $education) {
if($education['selected'] != 1) {
continue;
}
if(isset($final['key']) == FALSE
OR $education['key'] > $final['key']) {
$final = $education;
}
}
echo print_r($final, true);
Upvotes: 0