php过滤数组的数组只有一个属性值

时间:2017-01-03 21:19:30

标签: php arrays

我有一个php数组:

  $myarray = array(
  array(
    'id' = '1',
    'number' = '2'
  ),
  array(
    'id' = '1',
    'number' = '3'
  ),
  array(
    'id' = '2',
    'number' = '5'
  ),
  array(
    'id' = '2',
    'number' = '2'
  ),
 );

我需要过滤这个数组,只获得一个带有最大数值的'id'。

示例期望输出:

$myarray = array(
array(
    'id' = '1',
    'number' = '3'
  ),
  array(
    'id' = '2',
    'number' = '5'
  )
  );

我该如何过滤?

我试图循环它,但它没有用。

$array = array();
 for($i = 0; $i < count($myarray);$i++) {
//If not contains in $array , push to $array
                $array []['id'] = $myarray[$x]['id']; 
                $array []['number'] = $myarray[$x]['number']; 

          }

2 个答案:

答案 0 :(得分:0)

创建一个关键数组,其键为id,值包含原始数组中的最大number

$maxes = array();
foreach ($myarray as $el) {
    $id = $el['id'];
    $num = $el['number'];
    if (!isset($maxes[$id])) {
        $maxes[$id] = array('id' => $id, 'number' => $num);
    } elseif ($num > $maxes[$id]['number']) {
        $maxes[$id]['number'] = $number;
    }
}

答案 1 :(得分:0)

这是一个简单的类:

class FilterMax
    {
        private $temp  = [];
        private $array = [];

        public function __construct($array)
        {
            if ( !is_array($array)) {
                throw new InvalidArgumentException('Array should be an array');
            }
            $this->array = $array;
        }

        public function getFilteredResults($searchKey = 'id', $searchValue = 'number')
        {
            foreach ($this->array as $index => $item) {
                if ( !isset($item[ $searchKey ]) || !isset($item[ $searchValue ])) {
                    throw new Exception('Key or value does not exists in array');
                }
                $itemKey = $item[ $searchKey ];
                if ( !isset($this->temp[ $itemKey ])) {
                    $this->temp[ $itemKey ] = $index;
                }
                else {
                    $itemValue = $item[ $searchValue ];
                    $tempIndex = $this->temp[ $itemKey ];
                    $tempValue = $this->array[ $tempIndex ][ $searchValue ];
                    if ($itemValue > $tempValue) {
                        unset($this->array[ $tempIndex ]);
                    }
                    else {
                        unset($this->array[ $index ]);
                    }
                }
            }

            return $this->array;
        }
    }

拿你的阵列

   $myarray = [
                [
                    'id'     => '1',
                    'number' => '2',
                ],
                [
                    'id'     => '1',
                    'number' => '3',
                ],
                [
                    'id'     => '2',
                    'number' => '5',
                ],
                [
                    'id'     => '2',
                    'number' => '2',
                ],
            ];

并像这样使用它:

            $filterMax = new FilterMax($myarray);
            $result    = $filterMax->getFilteredResults('id', 'number');
相关问题