我目前正在尝试创建一个具有二维的关联数组,我认为针对此问题的解决方案可以解决具有更大维度的数组的问题。
我使用如下所示的API恢复数据:
{
"item_id": "89",
"name": "Confiture de Myrtilles",
"product_id": "737",
"meta_key": "vmm_warehouse_sg_10783",
"meta_value": "0"
},
{
"item_id": "89",
"name": "Confiture de Myrtilles",
"product_id": "737",
"meta_key": "vmm_warehouse_sg_10782",
"meta_value": "0"
},
{
"item_id": "91",
"name": "Poires Guyot (bio)",
"product_id": "690",
"meta_key": "_backorders",
"meta_value": "no"
},
{
"item_id": "91",
"name": "Poires Guyot (bio)",
"product_id": "690",
"meta_key": "_sold_individually",
"meta_value": "no"
},
我只想创建一个像这样的数组:
array[item_id->[meta_key->meta_value]]
因此,我必须恢复将具有第二个数组作用的item_id,并在该数组中放置关联的meta_key和meta_value之后。
因此,例如,我将有一个像这样的数组:
产品[89] [“ vmm_warehouse_sg_10783”->“ 0” “ vmm_warehouse_sg_10782”->“ 0”]
和另一个类似的东西:
产品[91] [........]
最后,我将有一个像这样的最终数组:
Products [ [89]->{"vmm_warehouse_sg_10783"->"0","vmm_warehouse_sg_10782"->"0"}
[91]->{.....}]
我已经尝试过一些东西,但是我只是一个初学者,我没有找到解决问题的方法。
$Products = $this->wpdb->get_results( $SQL_Deliveries );
//this line allow $Products to recover all data from the API
foreach ( $Products as $Product ) {
$Meta_products[] = Product->item_id;
foreach($Product as $Product_meta){
$Meta_products[$item_id]->{Product_meta->meta_key,Product_meta
->meta_value);
}
我确定我也确实在代码中犯了错误,但是我真的不知道如何解决这个问题。谢谢您的参与!
答案 0 :(得分:1)
您似乎想要一个多维对象数组。
需要一些麻烦来声明嵌套对象。花括号也是必要的。
代码:(Demo)
$products = [
(object)["item_id" => "89",
"name" => "Confiture de Myrtilles",
"product_id" => "737",
"meta_key" => "vmm_warehouse_sg_10783",
"meta_value" => "0"
],
(object)["item_id" => "89",
"name" => "Confiture de Myrtilles",
"product_id" => "737",
"meta_key" => "vmm_warehouse_sg_10782",
"meta_value" => "0"
]
];
$result = (object)[];
foreach($products as $product) {
if (!isset($result->{$product->item_id})) {
$result->{$product->item_id} = (object)[];
}
$result->{$product->item_id}->{$product->meta_key} = $product->meta_value;
}
var_export($result);
输出:
(object) array(
'89' =>
(object) array(
'vmm_warehouse_sg_10783' => '0',
'vmm_warehouse_sg_10782' => '0',
),
)
或者,要生成嵌套对象结构,可以构建一个数组数组,然后在结果上使用json_encode()
,然后使用json_decode()
。
如果要将数组作为输出,这是最简单的: 代码:(Demo)
$products = [
(object)["item_id" => "89",
"name" => "Confiture de Myrtilles",
"product_id" => "737",
"meta_key" => "vmm_warehouse_sg_10783",
"meta_value" => "0"
],
(object)["item_id" => "89",
"name" => "Confiture de Myrtilles",
"product_id" => "737",
"meta_key" => "vmm_warehouse_sg_10782",
"meta_value" => "0"
],
(object)["item_id" => "91",
"name" => "Poires Guyot (bio)",
"product_id" => "690",
"meta_key" => "_backorders",
"meta_value" => "no"
],
(object)["item_id" => "91",
"name" => "Poires Guyot (bio)",
"product_id" => "690",
"meta_key" => "_sold_individually",
"meta_value" => "no"
]
];
$result = [];
foreach($products as $product) {
$result[$product->item_id][$product->meta_key] = $product->meta_value;
}
var_export($result);
输出:
array (
89 =>
array (
'vmm_warehouse_sg_10783' => '0',
'vmm_warehouse_sg_10782' => '0',
),
91 =>
array (
'_backorders' => 'no',
'_sold_individually' => 'no',
),
)
答案 1 :(得分:0)
我不完全了解您要实现的目标,但是我想您要使用以id为键的关联数组和以meta_key和meta_value为值的数组?
如果是,则:
foreach ( $Products as $Product ) {
$Meta_products[$Product->item_id][$Product->meta_key] = $Product->meta_value;
}