下面是将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
) ....我想将Array中的数据过滤为ZONE = '03‘。
有没有人能给出这样做的示例代码?
谢谢你。
发布于 2015-03-13 12:56:07
您可以使用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。
https://stackoverflow.com/questions/29025075
复制相似问题