带有数组的PHP stdClass对象

时间:2017-10-25 21:48:22

标签: php arrays clio-api

我有这个数组。我尝试了一些但没有得到我想要的东西。 我尝试了一个foreach循环,但它似乎并不容易做到这个过程需要很长时间。

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [display_number] => 100140
                    [client] => stdClass Object
                        (
                            [name] => TAUQIR SHEIKH ET AL
                        )

                )

            [1] => stdClass Object
                (
                    [display_number] => 100141
                    [client] => stdClass Object
                        (
                            [name] => YOLANDA SHEIKH ET AL
                        )

                )

我希望它是一个简单的数组

[0] => Array
        (
            [0] => 100140
            [1] => TAUQIR SHEIKH ET AL
        )

    [1] => Array
        (
            [0] => 100141
            [1] => YOLANDA SHEIKH ET AL
        )

好的旧代码可以工作,但现在他们更新了API,这使情况变得更糟。现在回复

(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [data] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [display_number] => 100140
                                    [client] => stdClass Object
                                        (
                                            [name] => TAUQIR SHEIKH ET AL
                                        )

                                )

                            [1] => stdClass Object
                                (
                                    [display_number] => 100141
                                    [client] => stdClass Object
                                        (
                                            [name] => YOLANDA SHEIKH ET AL
                                        )

                                )

我用新代码尝试了这个...但是数组是空的。我哪里错了?

//clean and make into an array
        $matter_array = array();     
        if(!empty($response_Decode->data->data) && 
         is_array($response_Decode->data->data)) {
            foreach ($response_Decode->data->data as $info) {
                $d = array();
                $d[] = $info->display_number;
                $d[] = $info->client->name;
                $matter_array[] = $d;
            }
        }

        print_r($matter_array);  //For testing
        die(); //For testing

3 个答案:

答案 0 :(得分:2)

我建议先询问谁/什么进程填充数据集,然后在那里进行调整。

如果不可用,则需要循环。

$results = array();     
if(!empty($object->data) && is_array($object->data)) {
    foreach ($object->data as $info) {
        $d = array();
        $d[] = $info->display_number;

        if(!empty($object->client)) {
            $d[] = $object->client->name;
        }

        $results[] = $d;
    }
}

print_r($results);

我是空的()偏执狂。代码未经过测试,但应该让您走上正确的轨道。

答案 1 :(得分:0)

所以你很亲密......谢谢你。

//clean and make into an array
    $matter_array = array();     
    if(!empty($resp->data) && is_array($resp->data)) {
        foreach ($resp->data as $info) {
            $d = array();
            $d[] = $info->display_number;
            $d[] = $info->client->name;
            $matter_array[] = $d;
        }
    }

    print_r($matter_array)

答案 2 :(得分:0)

好的,所以我只是简化了阵列...而且它的工作原理!

//clean and make into an array
        $response_Decode=$response_Decode->data;
        $response_Decode=$response_Decode[0];
        //print_r ($response_Decode);
        //die(); //For testing
        $matter_array = array();     
        if(!empty($response_Decode->data) && is_array($response_Decode->data)) {
            foreach ($response_Decode->data as $info) {
                $d = array();
                $d[] = $info->display_number;
                $d[] = $info->client->name;
                $matter_array[] = $d;
            }
        }