php - 如何在多维数组中插入 foreach 循环

php - How to insert a foreach loop inside a multidimensional array

如何在多维数组中插入 foreach 循环?

我有一个多维数组,用于将我的网站连接到 Mailchimp。我必须使用 foreach 循环检查用户购买的产品数量,并将这些添加到数组调用 "lines".

这是我目前的 json 代码,在我发送给 Mailchimp 之后:

$json_data = '{
      "id": "2'. $order->id .'",
      "customer": {
        "id": "71",
        "email_address": "'.$order->email.'",
        "opt_in_status": true,
        "company": "'.$order->company_name.'",
        "first_name": "'.$order->pad_firstname.'",
        "last_name": "'.$order->pad_lastname.'",
        "orders_count": 1,
        "total_spent": 86
      },
      "checkout_url": "https://www.mywebsite.it/en/checkout/confirmation/",
      "currency_code": "EUR",
      "order_total": 86,
        "lines"[{
            '.$line_result.'
        }]
    }';

$line_result 是我尝试添加产品数组的地方。 我知道错了。

"lines" 中的所有数组需要像这样:

"lines":[
        {
            data product 1 ...
        },
        {
            data product 2 ...
        }
]

这是我的foreach:

foreach ($order->pad_products as $product) {
        $line_result['line'] = array(
            "id" => "$order->id",
            "product_id" => "$product->pad_product_id",
            "product_title" => "$product->title",
            "product_variant_id" => "$product->id",
            "product_variant_title" => "$product->title",
            "quantity" => "$product->pad_quantity",
            "price" => "$product->prezzo",
        );
    };

插入此数据并创建我需要的多维数组的正确方法是什么? 谢谢。

您只需将所有 $line_result 存储在全局变量中,然后将其绑定到您的 json 模型:

$results = [];

foreach ($order->pad_products as $product) {
    $results[] = array(
        "id" => $order->id,
        "product_id" => $product->pad_product_id,
        "product_title" => $product->title,
        "product_variant_id" => $product->id,
        "product_variant_title" => $product->title,
        "quantity" => $product->pad_quantity,
        "price" => $product->prezzo,
    );
};

$data = json_decode($json_data, true);
$data['lines'] = $results;
$json = json_encode($data);

编辑:脚本数组到json

$lines = [];

foreach ($order->pad_products as $product) {
    $lines[] = array(
        "id" => $order->id,
        "product_id" => $product->pad_product_id,
        "product_title" => $product->title,
        "product_variant_id" => $product->id,
        "product_variant_title" => $product->title,
        "quantity" => $product->pad_quantity,
        "price" => $product->prezzo,
    );
}

$data = [
    'id' => '2'.$order->id,
    'customer' => [
        'id' => '71',
        'email_address' => $order->email,
        'opt_in_status' => true,
        'company' => $order->company_name,
        'first_name' => $order->pad_firstname,
        'last_name' => $order->pad_lastname,
        'orders_count' => 1,
        'total_spent' => 86
    ],
    'checkout_url' => 'https://www.mywebsite.it/en/checkout/confirmation',
    'currency_code' => 'EUR',
    'order_total' => 86,
    'lines' => $lines
];

$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);

我要感谢@adrianRosi 的帮助和他给我的意见。

最后我找到了我的解决方案,它是 json_encode 以 json 格式添加到 $data 之前的数组。

这样:

            $product_list = [];    

            foreach ($order->pad_products as $product) {
                $product_list[] = array(
                    "id" => "$id",
                    "..." => "...",
                );
            };

            $data_products['lines'] = $product_list;
            $json_products = json_encode($data_products);
            $json_products_edit = substr($json_products, 1, -1); // to delete the {}

            $prezzo_totale = $order->pad_price_total;

            $json_data = '{
                ...
                ...
                '.$json_products_edit.'
            }';