How to filer PHP Array stdClass Object

2020-01-20 06:07发布

问题:

I have data below in the array that I got from JSON like this

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

and then I have data like this when I 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
            )
        )
)

I'd like to filter data in the array as ZONE='03'
and I've tried this code with array_filter() but noting.

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

Can anyone help or suggest me to do this?
Thank you.

回答1:

Pretty straightforward. You're trying to array_filter something that isn't really an array (or at least the array you're attempting to iterate over).

You want to run something more like this:

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