4

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

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

This function was taken from: if-not-true-then-false.com/2009/…
it has been slightly changed I think, so that it does not use the FUNCTION magic constant
This doesn't work for me. I get error: "Undefined variable: this"... but I just realised that's because this version was written for use as a method in a class. Version below (FAngel) is useful if you want to use this as a non-classed function.
3

Try a code below. Suppose problem is because of this line: $this->d = get_object_vars($d);. You put get_object_vars() result into $this->d and return unchanged $d;

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(__FUNCTION__, $d);
                }
                else {
                    // Return array
                    return $d;
                }
            }

3 Comments

Thanks @FAngel Got a following response 'Warning: array_map() expects parameter 1 to be a valid callback, function 'objectToArray' not found or invalid function name in C:\xampp\htdocs\msc\SourcesClasses.php on line 151 NULL code returns an array of results' Line 151 ' return array_map(FUNCTION, $d);'
Put it outside the class or take a look at this page. Frankly speaking never tried to do something like this, but as I understand you need to replace return array_map(__FUNCTION__, $d); with return array_map(array($this, 'objectToArray'), $d);
Thanks @ FAngel, I think we are getting closer - with this change the fynction returns the array of objects, but what I need really is a normal associative array that is outputed by the test code I linked to in the first post (Amazon Library and the site with function). Any Ideas for the fix? Best, Adam
1

I couldn't solve this either but found an alternative solution in this thread: Convert multidimensional objects to array.

It's shorter, and there's no conflict within the class. The discussion is above, and here's the code:

$array = json_decode(json_encode($object), true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.