PHP 来自 API 调用的对象数组
PHP Object Array from API call
我进行了 API 调用 $client->orders->get()
,我收到了回复,其中的数据格式如下:
Array ( [0] => stdClass Object ( [id] => 5180 [order_number] => 5180 [created_at] => 2016-12-21T14:50:08Z [updated_at] => 2016-12-21T15:01:51Z [completed_at] => 2016-12-21T15:01:52Z [status] => completed [currency] => GBP [total] => 29.00 [subtotal] => 29.00 [total_line_items_quantity] => 1 [total_tax] => 0.00 [total_shipping] => 0.00 [cart_tax] => 0.00 [shipping_tax] => 0.00 [total_discount]...........
所以我循环遍历数据:
foreach ( $client->orders->get() as $order ) {
// skip guest orders (e.g. orders with customer ID = 0)
print_r($order);
echo $order[0];
echo $order->subtotal;
}
我遇到的问题是输出数据,如果我使用 print_r 函数,我会得到一个输出,但我不知道如何访问数组的各个元素。
如果我尝试:
echo $order[0].[id];
我得到:
可捕获的致命错误:class stdClass 的对象无法转换为字符串。
我对此进行了高低搜索,但就是找不到任何我理解的内容。请帮忙...:)
下面循环$order
foreach ( $client->orders->get() as $order ) { .. }
是属于这个$client->orders->get()
数组的每一个元素,它们是object的形式。
因此输出(例如 subtotal
条目)你做 echo $order->subtotal
:
foreach ( $client->orders->get() as $order ) { $order->subtotal }
echo $order[0]
(inside 循环)是无效的,因为这样做会将 $order
视为一个数组,同时它的类型是 对象。改为执行 $order->somefields
以引用其属性。
print_r($order)
(inside 循环)是有效的,因为您可以使用 print_r 打印属性对象。
$client->orders->get()[0]->subtotal
(如果把放在循环之外)也是有效的。
以防万一数组的元素同时包含 object 或 array
$myarray = $client->orders->get();
// True because the first element is object
echo (is_object($myarray[0])) ? 'True':'False';
// False because the first element is object, not an array
echo is_array($myarray[0]) ? 'True':'False';
为此
// the same with echo is_array($client->orders->get()) ? 'True':'False';
echo is_array($myarray) ? 'True':'False';
肯定是这样,.
您可以在不使用 foreach 循环的情况下访问小计列值。这是代码。
$response = $client->orders->get();
$subtotal = $response[0]->subtotal;
如果你想使用 foreach 那么你可以这样访问。
foreach ( $client->orders->get() as $order ) {
echo $order->order_number;
}
我进行了 API 调用 $client->orders->get()
,我收到了回复,其中的数据格式如下:
Array ( [0] => stdClass Object ( [id] => 5180 [order_number] => 5180 [created_at] => 2016-12-21T14:50:08Z [updated_at] => 2016-12-21T15:01:51Z [completed_at] => 2016-12-21T15:01:52Z [status] => completed [currency] => GBP [total] => 29.00 [subtotal] => 29.00 [total_line_items_quantity] => 1 [total_tax] => 0.00 [total_shipping] => 0.00 [cart_tax] => 0.00 [shipping_tax] => 0.00 [total_discount]...........
所以我循环遍历数据:
foreach ( $client->orders->get() as $order ) {
// skip guest orders (e.g. orders with customer ID = 0)
print_r($order);
echo $order[0];
echo $order->subtotal;
}
我遇到的问题是输出数据,如果我使用 print_r 函数,我会得到一个输出,但我不知道如何访问数组的各个元素。
如果我尝试:
echo $order[0].[id];
我得到:
可捕获的致命错误:class stdClass 的对象无法转换为字符串。
我对此进行了高低搜索,但就是找不到任何我理解的内容。请帮忙...:)
下面循环$order
foreach ( $client->orders->get() as $order ) { .. }
是属于这个$client->orders->get()
数组的每一个元素,它们是object的形式。
因此输出(例如 subtotal
条目)你做 echo $order->subtotal
:
foreach ( $client->orders->get() as $order ) { $order->subtotal }
echo $order[0]
(inside 循环)是无效的,因为这样做会将$order
视为一个数组,同时它的类型是 对象。改为执行$order->somefields
以引用其属性。print_r($order)
(inside 循环)是有效的,因为您可以使用 print_r 打印属性对象。$client->orders->get()[0]->subtotal
(如果把放在循环之外)也是有效的。
以防万一数组的元素同时包含 object 或 array
$myarray = $client->orders->get();
// True because the first element is object echo (is_object($myarray[0])) ? 'True':'False';
// False because the first element is object, not an array echo is_array($myarray[0]) ? 'True':'False';
为此
// the same with echo is_array($client->orders->get()) ? 'True':'False';
echo is_array($myarray) ? 'True':'False';
肯定是这样,.
您可以在不使用 foreach 循环的情况下访问小计列值。这是代码。
$response = $client->orders->get();
$subtotal = $response[0]->subtotal;
如果你想使用 foreach 那么你可以这样访问。
foreach ( $client->orders->get() as $order ) {
echo $order->order_number;
}