简体   繁体   中英

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.

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. 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(). 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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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