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;
                }
            }
}
piokuc
  • 25,594
  • 11
  • 72
  • 102
Adam P
  • 139
  • 1
  • 10

3 Answers3

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

Adam P
  • 139
  • 1
  • 10
  • This function was taken from: http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ – hlscalon Jul 09 '14 at 16:40
  • it has been slightly changed I think, so that it does not use the _FUNCTION_ magic constant – mikey Jul 23 '14 at 01:35
  • 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. – geoidesic Apr 08 '16 at 16:10
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;
                }
            }
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • 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);' – Adam P Sep 03 '12 at 00:06
  • 2
    Put it outside the class or take a look at this [page](http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback). 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);` – Viktor S. Sep 03 '12 at 00:15
  • 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 – Adam P Sep 04 '12 at 00:16
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);
iehrlich
  • 3,572
  • 4
  • 34
  • 43
Brian Cugelman
  • 184
  • 1
  • 6