4

我对这个类有疑问,尤其是最后两个函数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;
                }
            }
}
4

3 回答 3

5

好的,修好了。

供将来参考:

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

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

感谢所有贡献者,

亚当

于 2012-09-06T15:22:52.500 回答
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;
                }
            }
于 2012-09-02T23:45:50.583 回答
1

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

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

$array = json_decode(json_encode($object), true);
于 2017-07-03T04:59:54.817 回答