2014-09-29 83 views
1

我有一個具有以下結構的數組,我需要從已選擇的子數組中選擇最大教育級別=> 1,[key]越大教育程度。 無論如何用PHP內置數組函數做到這一點?PHP根據多維數組中的值選擇數組

   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 
          ) 

        ) 

回答

0

當然。循環遍歷每個內部數組,並檢查它們的值與當前頂部的數值。例如: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); 
0

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')); 
0
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); 

只是不要忘了設置或設置$ret = false; //null, etc.. 如果你想如果你想使用PHP的內置插件多次:)

0

運行這段代碼,array_reduce可能是要走的路。這樣的事情應該做的伎倆:

$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 5.3的工作或更高版本,因爲它使用anonymous functions,這是不是在早期版本的PHP可用。如果你使用的是早期版本,你應該升級。但是如果你不能升級,那麼你必須將函數部分定義爲一個普通的獨立函數,然後將第二個參數中的函數名稱(作爲字符串)傳遞給array_reduce。該方法顯示在array_reduce的文檔頁面的示例中。

0

我已經爲你構建了一個測試函數。 經過測試和工作!歡呼到學士學位!

<?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; 
?>