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

Php 将stdObject转换为数组调用函数,php,oop,object,web,amazon-web-services,Php,Oop,Object,Web,Amazon Web Services,这个类有一个问题,尤其是最后两个函数callApi($query)和objectToArray($d)。它们的目标是从AmazonAPI返回的对象返回一个数组 我认为问题在于从一个函数调用另一个函数: $arrayResponse=$this->objectToArray($response); 即使我在var_dump$arrayResponse时这样做,我仍然将其作为对象获取,并且无法执行特定于数组的操作(duh!;)。我在和上找到了objectToArray() 当我单独测试它时,

这个类有一个问题,尤其是最后两个函数
callApi($query)
objectToArray($d)
。它们的目标是从AmazonAPI返回的对象返回一个数组

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

$arrayResponse=$this->objectToArray($response);
即使我在var_dump
$arrayResponse
时这样做,我仍然将其作为对象获取,并且无法执行特定于数组的操作(duh!;)。我在和上找到了
objectToArray()

当我单独测试它时,它工作得很好,但在将它放入项目后,我仍然得到了对象

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

提前谢谢大家,

亚当

类Amazon扩展源代码实现ICallsApi{
函数_u构造(){
$this->impact=55;
$this->side=“supply”;
回声“我是亚马逊”

”; 回声“和我的影响=“.this->impact.”

”; } 私有函数FormulateQuery($query){ echo“为AmazonAPI制定查询并返回”; } //从amazon返回给定关键字的数组记录 公共函数callApi($query) { $client=newamazonecs('dataRemoved','dataRemoved','co.uk','dataRemoved'); $response=$client->category('book')->search($query); //var_dump($response); //回声“;
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;
                }
            }
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;
                }
            }
//打印(回复); //回声“;
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;
                }
            }
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;
                }
            }
$arrayResponse=$this->objectToArray($response); var_dump($arrayResponse); //回声“;
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;
                }
            }
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;
                }
            }
//打印($arrayResponse); //回声“;
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;
                }
            }
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;
                }
            }
echo“代码返回结果数组”; //返回$arrayResponse; }//联机找到objectToArray($d) 公共职能部门反对Array($d){ if(is_object($d)){ //获取给定对象的属性 //使用get_object_vars函数 $this->d=获取对象变量($d); } if(is_数组($d)){ /* *返回转换为对象的数组 *使用函数(魔法常数) *用于递归调用 */ 返回数组映射(uuu函数,$d); } 否则{ //返回数组 返回$d; } } }
尝试下面的代码。假设问题是因为这一行:
$this->d=get\u object\u vars($d)。将
get\u object\u vars()
result放入$this->d并返回未更改的$d

$array = json_decode(json_encode($object), true);
好的,修好了

供日后参考:

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

感谢所有贡献者


Adam

我也无法解决此问题,但在此线程中找到了另一种解决方案:

它比较短,而且课堂上没有冲突。以上是讨论内容,下面是代码:


感谢@FAngel收到以下响应“警告:array_map()希望参数1是有效回调,在C:\xampp\htdocs\msc\SourcesClasses.php的第151行未找到函数“objectToArray”或函数名无效空代码返回结果数组“第151行”返回数组_map(函数,$d);”把它放在课外或者看看这个。坦率地说,我从未尝试过这样做,但据我所知,您需要替换
返回数组\u映射(\uuuu函数\uuu,$d)带有
返回数组映射(数组($this,'objectToArray'),$d)
谢谢@FAngel,我想我们离得越来越近了——有了这个变化,fynction返回了对象数组,但我真正需要的是一个普通的关联数组,它由我在第一篇文章中链接到的测试代码输出(Amazon库和带有函数的站点)。有什么解决办法吗?最棒的是,Adam这个函数取自:我想它已经稍微改变了,所以它没有使用magic Constant这个函数对我不起作用。我得到错误:“未定义的变量:这个”。。。但我刚刚意识到这是因为这个版本是为在类中用作方法而编写的。如果您想将其用作非类函数,下面的版本(FAngel)非常有用。