如何归档 PHP 数组标准类对象


How to filer PHP Array stdClass Object

我在从 JSON 获得的数组中有这样的数据

$page = file_get_contents("http://giswebcenter.mwa.co.th/mwa/ashx/Proxy.ashx");
$json_output = json_decode($page);

然后当我print_r($json_output)时我有这样的数据

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
            )
        [7] => stdClass Object
            (
                [BRANCH] => 01
                [ZONE] => 04
                [BLOCK] => 05
                [MATL] => SCP
                [LENGTH] => 19.979
            )
        [8] => stdClass Object
            (
                [BRANCH] => 01
                [ZONE] => 04
                [BLOCK] => 05
                [MATL] => ST
                [LENGTH] => 28.591
            )
        [9] => stdClass Object
            (
                [BRANCH] => 01
                [ZONE] => 04
                [BLOCK] => 07
                [MATL] => SCP
                [LENGTH] => 38.967
            )
        )
)

我想将数组中的数据过滤为 ZONE='03'
我已经用 array_filter() 尝试过这段代码,但请注意。

function filterZone($obj)
{
    return $obj['data']->BRANCH == "01";
}
$BRANCH = array_filter($json_output, 'filterZone');
print_r($BRANCH);

谁能帮助或建议我这样做?
谢谢。

很简单。 您正在尝试array_filter不是真正数组的东西(或者至少是您尝试迭代的数组)。

你想运行更多类似的东西:

function filterZone($obj)
{
    return $obj['ZONE'] == '03';
}
$BRANCH = array_filter($json_output->data, 'filterZone');
print_r($BRANCH);