创建并填充多个维度的关联数组

Create and fill an associative array of multiples dimensions

我目前正在尝试创建一个具有两个维度的关联数组,我认为这个问题的解决方案可以解决具有更多维度的数组的问题。

我用 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);
    }

我确定我的代码中也有错误,但我真的不知道如何解决这个问题。感谢您的参与!

我不完全明白你想要达到什么目的,但我想你想要以 id 作为键和 meta_key 和 meta_value 的数组作为其值的关联数组?

如果是,则:

foreach ( $Products as $Product ) {
    $Meta_products[$Product->item_id][$Product->meta_key] = $Product->meta_value;
}

看起来你想要一个多维对象数组。

声明嵌套对象涉及一些小动作。大括号也是必要的。

代码:(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',
  ),
)