将 stdObject 转换为数组 - 调用函数


Converting stdObject into an array - calling function

我对这个类有问题,尤其是最后 2 个函数callApi($query)objectToArray($d)。这些的目标是从 Amazon API 返回的对象返回一个数组。

我认为问题在于在这里从一个函数调用另一个函数:

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

即使我在var_dump $arrayResponse时这样做,我仍然将其作为对象获取并且无法执行特定于数组的操作(duh! ;) (。我在这个网站和亚马逊图书馆找到了objectToArray()

当我单独测试它时,它工作正常,但在将其放入项目中后,我仍然得到该对象。

是我只是以错误的方式调用函数,所以它不转换它吗?

提前谢谢你,

亚当

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

好的,固定的。

供将来参考:

将整个 std 对象转换为数组的工作函数如下所示:

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

感谢所有贡献者,

亚当

尝试下面的代码。假设问题是因为这一行:$this->d = get_object_vars($d); 。您将get_object_vars()结果放入 $this->d 中并返回不变$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;
                }
            }

我也无法解决这个问题,但在这个线程中找到了一个替代解决方案:将多维对象转换为数组。

它更短,并且类内没有冲突。上面的讨论,下面是代码:

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