2

我有这个数组。我尝试了一些东西,但没有得到我想要的。我尝试了一个 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
4

3 回答 3

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);

我对empty()很偏执。代码未经测试,但应该让你走上正轨。

于 2017-10-25T22:53:01.853 回答
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)
于 2017-10-26T17:58:07.880 回答
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;
            }
        }
于 2017-11-16T18:01:54.913 回答