【问题标题】:PHP select arrays based on value in a multidimensional arrayPHP根据多维数组中的值选择数组
【发布时间】:2014-11-24 08:05:35
【问题描述】:

我有一个具有以下结构的数组,我需要从已选择=>1 的子数组中选择最大的教育水平,[key] 越大,教育水平就越高。 无论如何要使用 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
                            )

                    )

【问题讨论】:

    标签: php arrays select multidimensional-array


    【解决方案1】:

    如果你想使用 php 内置,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 文档页面上的示例中。

    【讨论】:

      【解决方案2】:
      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.. 如果你想多次运行这段代码:)

      【讨论】:

        【解决方案3】:

        我已经为你构建了一个测试函数。 测试和工作!为学士学位干杯!

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

        【讨论】:

          【解决方案4】:

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

          获取最高值的key:

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

          【讨论】:

            【解决方案5】:

            当然。循环遍历每个内部数组,并根据当前顶部的值检查它们的值。例如: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);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多