【发布时间】:2016-12-13 12:42:09
【问题描述】:
我对 PHP 很陌生,我正在努力将一个对象转换为一个数组,以便我可以在我的代码中进一步使用该数组。
我的脚本从 Magento API 检索产品数据,在结果变量上使用 print_r 后显示如下结果:
现在我可以看到它以“array”开头,但是当我尝试对已分配结果的变量使用 echo 时,我收到错误 Catchable fatal error: Object of class stdClass could not be converted to string。
如何将对象转换为数组,然后拆分数组,以便将所有 sku 键的值作为数组作为最终结果?
我尝试在结果变量上使用array(),然后尝试使用数组函数对其进行操作,但无论我尝试什么,我都会收到错误,所以我的想法是我没有正确理解这一点。
感谢您提供的任何见解。如果您需要查看,我的代码如下:
<?php
// Global variables.
$client = new SoapClient('xxx'); // Magento API URL.
$session_id = $client->login('xxx', 'xxx'); // API Username and API Key.
// Filter where the 'FMA Stock' attribute is set to 'Yes'.
$fma_stock_filter = array('complex_filter'=>
array(
array('key'=>'fma_stock', 'value'=>array('key' =>'eq', 'value' => 'Yes')),
),
);
// Assign the list of products to $zoey_product_list.
$zoey_product_list = $client->catalogProductList($session_id, $fma_stock_filter);
echo $zoey_product_list[0];
?>
【问题讨论】:
-
您正在接收一个对象数组(参见
stdClass Object)。所以一个简单的类型转换应该适合你:$array = (array) $zoey_product_list[0]; -
谢谢,现在这更有意义了 :)