2

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
jbalter
  • 59
  • 6

3 Answers3

2

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.

JI-Web
  • 481
  • 6
  • 27
  • I wish they would give it to me clean. Thier API is really bad. CLIO. They really don't want you to use PHP but that is what I know best. That got me close. The name is missing. Array ( [0] => Array ( [0] => 100140 ) [1] => Array ( [0] => 100141 ) – jbalter Oct 26 '17 at 17:46
  • Here is what the code looks like //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; if(!empty($resp->client)) { $d[] = $resp->client->name; } $matter_array[] = $d; } } print_r($matter_array); – jbalter Oct 26 '17 at 17:48
0

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)
jbalter
  • 59
  • 6
0

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;
            }
        }
jbalter
  • 59
  • 6