Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting stdObject into an array - calling function

I have a problem with this class, especially with the last 2 functions callApi($query) and objectToArray($d). The goal of those is to return an array from an object returned by Amazon API.

I think the problem is in calling one function from another here:

$arrayResponse=$this->objectToArray($response);

Even if I do so when I var_dump $arrayResponse I still get it as an object and cannot perform array specific operations (duh! ;) ). I have found the objectToArray() on this site and Amazon Library.

When I tested it individually it works fine but after putting it into a project I still get the object.

Is it me just calling the function in a wrong way, so it does not convert it?

Thank you in advance,

Adam

class Amazon extends Source implements ICallsApi{
    function __construct(){
        $this->impact = 55;
        $this->side = "supply";
        echo "<p>I am Amazon</p>";
        echo "<p>and my impact = ". $this->impact."</p>";

    }

    private function FormulateQuery($query){
                echo "Formulate query for Amazon API and return it";
    }

    //Returns the array records from amazon of a given keyword      
    public function callApi($query)
            {
            $client = new AmazonECS('dataRemoved', 'dataRemoved', 'co.uk', 'dataRemoved');

            $response  = $client->category('Books')->search($query);
            //var_dump($response);
            //echo "<pre>";
            //print_r($response);
            //echo "</pre>";

            $arrayResponse=$this->objectToArray($response);
            var_dump($arrayResponse);
            //echo "<pre>";
            //print_r($arrayResponse);
            //echo "</pre>";
                echo "code returns an array of results";
            //  return $arrayResponse;
            }  //objectToArray($d) found online

        public  function objectToArray($d) {
                if (is_object($d)) {
                    // Gets the properties of the given object
                    // with get_object_vars function
                    $this->d = get_object_vars($d);
                }

                if (is_array($d)) {
                    /*
                    * Return array converted to object
                    * Using __FUNCTION__ (Magic constant)
                    * for recursive call
                    */
                    return array_map(__FUNCTION__, $d);
                }
                else {
                    // Return array
                    return $d;
                }
            }
}
like image 544
Adam P Avatar asked Jan 15 '23 16:01

Adam P


1 Answers

Ok, fixed.

For a future reference:

The working function that converts the whole std Object into an array looks like :

public function objectToArray($d) {
                if (is_object($d)) {
                    // Gets the properties of the given object
                    // with get_object_vars function
                    $d = get_object_vars($d);
                }

                if (is_array($d)) {
                    /*
                    * Return array converted to object
                    * Using __FUNCTION__ (Magic constant)
                    * for recursive call
                    */
                    return array_map(array($this, 'objectToArray'), $d);
                //$this->d = get_object_vars($d);
                }
                else {
                    // Return array
                    return $d;
                }
            }

Thank you to all contributors,

Adam

like image 100
Adam P Avatar answered Jan 22 '23 17:01

Adam P