-1

I have seen the solution to this post: Convert a PHP object to an associative array but it doesn't work for me. I tried several ways to typecast a stdclass object to an array but with no luck. I am trying to send some data from my database to another server situated in a different country. This is the way I am trying:

This is the function:

public function update($dir, $id, $input)
    {           
        $data = ['id'=>$id, 'data'=>$input];

        $json = $this->call('PUT', '/'.$dir, $data);

        return $json;
    }

And this is what I am trying:

    require_once('../Rest_smp.php');
    $id="1366";
    $data["operating_city_ids"] = [1,2,3];
    $employee_array = (array) $data;
    echo (new Rest_smp())->update('promoter', $id, $employee_array);

The result is this:

[871] => stdClass Object
                (
                    [id] => 1366
                    [name] => NAME NAME
                    [username] => 145576
                    [email] => EMAIL@YAHOO.COM
                    [spi_classified] => 
                    [country_id] => 5
                    [timezone_id] => 9
                    [language_id] => 1
                    [status ] => active
                    [operating_city_ids] => stdClass Object
                        (
                            [1] => 123
                        )

                    [address] => CAMIL RESSU
                    [zip] => 000000
                    [phone] => 0766XXXXXX
                    [birth] => 1990-07-05
                    [gender] => female
                    [height] => 180
                    [shirt_size] => L
                    [shoe_size] => 42
                    [jeans_size] => 36
                    [driver_license] => yes
                    [employment_start_date] => 
                    [employment_end_date] => 
                    [clients] => Array
                        (
                        )

                )

The problem is here:

[operating_city_ids] => stdClass Object
                            (
                                [1] => 123
                            )

It should be an array and the values should be 0=>1,1=>2,2=>3. They said to me that I should typecast the stdcass object but still doesn't work. Am I missing something?

Bibu
  • 201
  • 3
  • 10
  • 25

2 Answers2

0

the simplest way is to cast StdObject to array:

$result = (array)(new Rest_smp())->update('promoter', $id, $employee_array);
var_dump($result);

see https://3v4l.org/TpUUF

but I'd do that inside the update function, not outside:

function update(...) {
    ...

    return (array) $data;
}

You try to cast an array to array, this has no effect:

$employee_array = (array) $data;

If you are using json_decode then you need to pass true as the second argument, see https://www.php.net/manual/de/function.json-decode.php

CodeXP
  • 153
  • 1
  • 10
  • tried all of this and still getting: [operating_city_ids] => stdClass Object ( [1] => 123 ) – Bibu Oct 14 '19 at 09:38
0

Try decoding your json_decode with the second argument as TRUE where returned objects will be converted into associative arrays.

Examples of the difference between using and not using that flag:

<?php

$data = 
[
    ['fruit'=>'apple', 'nut' => 'almond']
];

var_dump($json = json_encode($data));
$decoded = json_decode($json);
var_dump($decoded);
$decoded_array = json_decode($json, TRUE);
var_dump($decoded_array);

Output (with added line breaks):

string(34) "[{"fruit":"apple","nut":"almond"}]"

array(1) {
  [0]=>
  object(stdClass)#1 (2) {
    ["fruit"]=>
    string(5) "apple"
    ["nut"]=>
    string(6) "almond"
  }
}


array(1) {
  [0]=>
  array(2) {
    ["fruit"]=>
    string(5) "apple"
    ["nut"]=>
    string(6) "almond"
  }
}
Progrock
  • 7,373
  • 1
  • 19
  • 25