简体   繁体   中英

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

How to insert a foreach loop inside a multidimensional array ?

I have a multidimensional array that I use to connect my website to Mailchimp. I have to check with a foreach loop the number of products that the user buys, and add these insiede a array call "lines".

This is at moment my json code, that after I will send to 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.'
        }]
    }';

The $line_result is where I try to add the array of the products. I know is wrong.

all the array inside the "lines" need be like this:

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

This is my 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",
        );
    };

what is the correct way to insert this data and create a multidimensional array like the one I need? Thank you.

You just need to store all $line_result in global variable, and then, bind it to your json model :

$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);

EDIT : Script array to 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);

I want say thank you to @adrianRosi for the help and the input he gives me.

In the end I find my solution, that it's json_encode the array before add into $data in json format.

in this way:

            $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.'
            }';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM