简体   繁体   English

PHP stdClass对象内部具有数组

[英]PHP stdClass Object with array inside

I have this array. 我有这个数组。 I have tried a few things but not getting what I want. 我尝试了几件事,但没有得到想要的东西。 I tried a foreach loop but it does not seem to do it easly and the process takes a long time. 我尝试了一个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
                        )

                )

I want it to be one simple array 我希望它成为一个简单的数组

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

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

Ok So the old code works but now they updated the API and this made it worse. 好的,原来的代码可以工作,但是现在他们更新了API,这使情况变得更糟。 The response is now 现在的回应

(
    [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
                                        )

                                )

I tried this with the new code... But the array is empty. 我用新代码尝试了一下...但是数组为空。 Where am I going wrong? 我要去哪里错了?

//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

I would recommend asking who/what process is populating your dataset first and possibly adjust it there. 我建议先询问谁/什么过程正在填充您的数据集,并可能在那里进行调整。

If not available then looping is required. 如果不可用,则需要循环。

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

I'm paranoid with empty(). 我对empty()抱有偏执。 Code not tested but should get you on the right track. 代码未经测试,但可以使您步入正轨。

SO you were close... Thank you. 所以你很近...谢谢。

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

Ok so I just simplified the array... AND THAT WORKS! 好的,所以我只是简化了阵列...而已!

//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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM