如何在php中针对数组的多个实例


How to target multiple instances of an array in php?

我在定义我要找的东西时遇到了一些困难,所以希望我听起来不会太神秘。

我正试图从我的网店中从数组中获取一些内容,该数组存储订单中每个项目的订单信息。

我想返回此订单信息中的一些值。但我在确定正确的信息时遇到了一些困难。由于我想为每个单独的order_item返回这些信息,所以我需要针对唯一的键,并可能为每个函数编写一个。我不知道从哪里开始。

我当前返回的数组看起来像这样。例如,我该如何返回两个product_id?

array(2) {
["d4650547c8d3536a6741b300f563a8fb"]=>
array(11) {
["product_id"]=>
int(259)
["variation_id"]=>
int(278)
["variation"]=>
array(1) {
["pa_afmetingen-liggend"]=>
string(4) "m011"
}
["quantity"]=>
int(1)
["data"]=>
object(WC_Product_Variation)#3243 (24) {  ["variation_id"]=>
int(278)
["parent"]=>
}
["product_type"]=>
string(8) "variable"
}
array(2) {
["893hg547c8d35pga6741b300f56754ud"]=>
array(11) {
["product_id"]=>
int(279)
["variation_id"]=>
int(298)
["variation"]=>
array(1) {
["pa_afmetingen-liggend"]=>
string(4) "m011"
}
["quantity"]=>
int(1)
["data"]=>
object(WC_Product_Variation)#3243 (24) {  ["variation_id"]=>
int(298)
["parent"]=>
}
["product_type"]=>
string(8) "variable"
}

你在找这样的东西吗?

代码

<?php
    // Sample products Array
    $my_products = array();
    $my_products[] = array('product_id' => 230, 'product_name' => 'audi');
    $my_products[] = array('product_id' => 355, 'product_name' => 'benz');
    // My products
    print_r($my_products);
    $product_ids = array();
    foreach ($my_products as $product) {
      $product_ids[] = $product['product_id'];
    }
    // MY product ids
    print_r($product_ids);
    // My first product id
    echo $my_products[0]['product_id'];
    // My second product id
    echo $my_products[1]['product_id'];
?>

输出

// My products
      Array
    (
        [0] => Array
            (
                [product_id] => 230
                [product_name] => audi
            )
        [1] => Array
            (
                [product_id] => 355
                [product_name] => benz
            )
    )
// MY product ids
Array
(
    [0] => 230
    [1] => 355
)
// My first product id
230
// My second product id
355