使用关联数组
Using Associative arrays
我正在尝试访问另一个数组内部的关联数组的位置。
这是我的数组:
Array
(
[order_data] => stdClass Object
(
[id_order] => 2
[id_client] => 1
[date_order] => 2022-04-03 18:43:18
[address] => meu puto
[city] => Rabinho
[email] => 40834@aeds.pt
[cellphone] => 919788427
[code_order] => QX669156
[status] => PENDIG
[message] =>
[created_at] => 2022-04-03 18:43:18
[updated_at] => 2022-04-03 18:43:18
)
[products_order] => Array
(
[0] => stdClass Object
(
[id_order_product] => 8
[id_order] => 2
[name_product] => Green Tshirt
[price_unity] => 35.15
[quantity] => 1
[created_at] => 2022-04-03 18:43:18
)
)
)
我正在尝试访问名称为 name_product 的字段
这是我的代码:
$image = $order_details['products_order']['name_product']);
这给了我以下错误
Undefined array key "name_product"
可能只是一件简单的事情。
$image = $order_details['products_order'][0]);
看到[0] => stdClass Object
中的[0]了吗?
以下应该有效...
$image = $order_details['products_order'][0]['name_product'];
数据结构是一个array of array of object
这应该适合你:
$image = $order_details['products_order'][0]->name_product;
您不能像通常访问数组那样访问您的数据,正如@Omar_Tammam所说您正在使用数组对象。
以下示例说明了您的数据是如何设置的以及如何使用它:
# Creating your stdclass Object Array
$order_details['order_data'] = new stdClass();
$order_details['order_data']->id_order = 2;
$order_details['order_data']->id_client = 1;
$order_details['order_data']->date_order = "2022-04-03 18:43:18";
$order_details['order_data']->address = "meu puto";
$order_details['order_data']->city = "Rabinho";
$order_details['order_data']->email = "40834@aeds.pt";
$order_details['order_data']->cellphone = "919788427";
$order_details['order_data']->code_order = "QX669156";
$order_details['order_data']->status = "PENDING";
$order_details['order_data']->message = "";
$order_details['order_data']->created_at = "2022-04-03 18:43:18";
$order_details['order_data']->updated_at = "2022-04-03 18:43:18";
$order_details['products_order'][0] = new stdClass();
$order_details['products_order'][0]->id_order_product = "8";
$order_details['products_order'][0]->id_order = "2";
$order_details['products_order'][0]->name_product = "Green Tshirt";
$order_details['products_order'][0]->price_unity = "35.15";
$order_details['products_order'][0]->quantity = "1";
$order_details['products_order'][0]->created_at = "2022-04-03 18:43:18";
# To print all the data as example, showing stdClass Objects can
# be accessed through a foreach similar to an array
// foreach($order_details as $od){
// echo PHP_EOL, "_________________________", PHP_EOL;
// foreach($od as $a){
// if(!is_object($a)) echo $a, PHP_EOL;
// else foreach($a as $o) echo $o, PHP_EOL;
// }
// echo PHP_EOL, "_________________________", PHP_EOL;
// }
# this is wrong & will not work:
echo $order_details['products_order'][0]['name_product'];
# you would want to use this:
echo $order_details['products_order'][0]->name_product;
# more examples:
echo $order_details['order_data']->address;
echo $order_details['order_data']->city;
echo $order_details['order_data']->email;
echo $order_details['products_order'][0]->created_at;
# or as you wanted:
$image = $order_details['products_order'][0]->name_product;
echo $image;
# result would be: Green Tshirt
在线沙盒示例:https://onlinephp.io/c/76370
我还创建了一个递归函数,通过引用将数组与 stdClass 相互转换:
function array_to_stdClass(&$array){
$tmp = $array;
$array = new stdClass();
foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
return $array;
}
function stdClass_to_array(&$stdClass){
$tmp = $stdClass;
$stdClass = array();
foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
return $stdClass;
}
在线沙盒示例:https://onlinephp.io/c/4b183
我正在尝试访问另一个数组内部的关联数组的位置。
这是我的数组:
Array
(
[order_data] => stdClass Object
(
[id_order] => 2
[id_client] => 1
[date_order] => 2022-04-03 18:43:18
[address] => meu puto
[city] => Rabinho
[email] => 40834@aeds.pt
[cellphone] => 919788427
[code_order] => QX669156
[status] => PENDIG
[message] =>
[created_at] => 2022-04-03 18:43:18
[updated_at] => 2022-04-03 18:43:18
)
[products_order] => Array
(
[0] => stdClass Object
(
[id_order_product] => 8
[id_order] => 2
[name_product] => Green Tshirt
[price_unity] => 35.15
[quantity] => 1
[created_at] => 2022-04-03 18:43:18
)
)
)
我正在尝试访问名称为 name_product 的字段 这是我的代码:
$image = $order_details['products_order']['name_product']);
这给了我以下错误
Undefined array key "name_product"
可能只是一件简单的事情。
$image = $order_details['products_order'][0]);
看到[0] => stdClass Object
中的[0]了吗?
以下应该有效...
$image = $order_details['products_order'][0]['name_product'];
数据结构是一个array of array of object
这应该适合你:
$image = $order_details['products_order'][0]->name_product;
您不能像通常访问数组那样访问您的数据,正如@Omar_Tammam所说您正在使用数组对象。
以下示例说明了您的数据是如何设置的以及如何使用它:
# Creating your stdclass Object Array
$order_details['order_data'] = new stdClass();
$order_details['order_data']->id_order = 2;
$order_details['order_data']->id_client = 1;
$order_details['order_data']->date_order = "2022-04-03 18:43:18";
$order_details['order_data']->address = "meu puto";
$order_details['order_data']->city = "Rabinho";
$order_details['order_data']->email = "40834@aeds.pt";
$order_details['order_data']->cellphone = "919788427";
$order_details['order_data']->code_order = "QX669156";
$order_details['order_data']->status = "PENDING";
$order_details['order_data']->message = "";
$order_details['order_data']->created_at = "2022-04-03 18:43:18";
$order_details['order_data']->updated_at = "2022-04-03 18:43:18";
$order_details['products_order'][0] = new stdClass();
$order_details['products_order'][0]->id_order_product = "8";
$order_details['products_order'][0]->id_order = "2";
$order_details['products_order'][0]->name_product = "Green Tshirt";
$order_details['products_order'][0]->price_unity = "35.15";
$order_details['products_order'][0]->quantity = "1";
$order_details['products_order'][0]->created_at = "2022-04-03 18:43:18";
# To print all the data as example, showing stdClass Objects can
# be accessed through a foreach similar to an array
// foreach($order_details as $od){
// echo PHP_EOL, "_________________________", PHP_EOL;
// foreach($od as $a){
// if(!is_object($a)) echo $a, PHP_EOL;
// else foreach($a as $o) echo $o, PHP_EOL;
// }
// echo PHP_EOL, "_________________________", PHP_EOL;
// }
# this is wrong & will not work:
echo $order_details['products_order'][0]['name_product'];
# you would want to use this:
echo $order_details['products_order'][0]->name_product;
# more examples:
echo $order_details['order_data']->address;
echo $order_details['order_data']->city;
echo $order_details['order_data']->email;
echo $order_details['products_order'][0]->created_at;
# or as you wanted:
$image = $order_details['products_order'][0]->name_product;
echo $image;
# result would be: Green Tshirt
在线沙盒示例:https://onlinephp.io/c/76370
我还创建了一个递归函数,通过引用将数组与 stdClass 相互转换:
function array_to_stdClass(&$array){
$tmp = $array;
$array = new stdClass();
foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
return $array;
}
function stdClass_to_array(&$stdClass){
$tmp = $stdClass;
$stdClass = array();
foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
return $stdClass;
}
在线沙盒示例:https://onlinephp.io/c/4b183