如何过滤标准类对象


How can I filter stdClass object

>我在下面有将 JSON 放入数组的数据

stdClass Object
(
    [success] => 1
    [total] => 850
    [message] => 
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 03
                    [BLOCK] => 04
                    [MATL] => ST
                    [LENGTH] => 516.492
                )
            [1] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 03
                    [BLOCK] => 05
                    [MATL] => SCP
                    [LENGTH] => 19.177
                )
            [2] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 03
                    [BLOCK] => 05
                    [MATL] => ST
                    [LENGTH] => 519.355
                )
            [3] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 03
                    [BLOCK] => 06
                    [MATL] => SCP
                    [LENGTH] => 59.713
                )
            [4] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 03
                    [BLOCK] => 06
                    [MATL] => ST
                    [LENGTH] => 476.866
                )
            [5] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 04
                    [BLOCK] => 03
                    [MATL] => SCP
                    [LENGTH] => 64.875
                )
            [6] => stdClass Object
                (
                    [BRANCH] => 01
                    [ZONE] => 04
                    [BLOCK] => 03
                    [MATL] => ST
                    [LENGTH] => 44.888
                ) ....

我想将数组中的数据过滤为 ZONE = '03'。
任何人都可以给出示例代码来执行此操作吗?
谢谢。

你可以

利用array_filter http://php.net/manual/en/function.array-filter.php

$input = (object)array(
  'success'=>1,
  'total'=>850,
   'data'=>array(
    (object)array('ZONE'=>'01'),
    (object)array('ZONE'=>'04'),
    (object)array('ZONE'=>'04'),
    (object)array('ZONE'=>'04'),
    (object)array('ZONE'=>'03'),
    (object)array('ZONE'=>'02')
  )
);
$output = array_filter($input->data,function($object){
  return $object->ZONE == '04';
});
print_r($output);

array_filter通过测试数组中的每个项目并返回 TRUE 或 FALSE 来工作。