【发布时间】:2015-09-30 19:10:35
【问题描述】:
我正在使用 SOAP V2 API 从 Magento 实例中提取数据。 Magento 将我的请求结果作为多维数组返回,我将其编码为 json 以帮助解析为 html。但是,当我遇到这种格式时:
array(
array(
array()
)
)
我收到通知:数组到字符串转换错误。
这是我的代码:
//Make sure a query is set
if (!empty($_GET['q'])) {
$getQuery = $_GET['q'];
//Requests
switch ($getQuery) {
case 'allorders':
$result = json_decode(json_encode($client->salesOrderList($session)), true);
break;
case 'inventory':
$result = json_decode(json_encode($client->catalogInventoryStockItemList($session, array(695, 694, 693, 692))), true);
break;
case 'products':
$result = json_decode(json_encode($client->catalogProductList($session)), true);
break;
}
//if (!empty($result)) {
?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($result))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row): $row; ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
//}
} else {
echo "No Results Found";
}
// Close the session
$client->endSession($session);
错误发生在 for 循环中的第二次内爆。
更新 14:45pm CST
这是实际输出的样子
Array
(
[0] => Array
(
[product_id] => 481
[sku] => 012
[name] => Twill Cap
[set] => 9
[type] => simple
[category_ids] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 11
)
[website_ids] => Array
(
[0] => 2
)
)
)
【问题讨论】:
-
在处理 implode() 之前只需进行 is_array() 检查
-
不起作用。它没有显示部分信息,而是显示相同的错误。
-
你能检查 $result 的内容是什么吗?使用调试器尝试找出它何时出错。
-
我添加了一个示例,说明在数组上执行 print_r 后的数据输出。
-
所以
category_ids是一个数组。将其与其他字符串内爆会导致通知。