简体   繁体   中英

foreach loop inside multi-dimensional array

I have an array $new_items with following output in print_r:

    Array
    (
        [0] => Array
            (
                [id] => 70494
                [weight] => 0.3000
                [Price] => 31.4400
                [url] => http://www.myshop.com
                [name] => Apple iphone
                [category] => Array
                    (
                        [parent_id] => Array
                            (
                                [0] => 53
                                [1] => 81
                            )

                        [parent_name] => Array
                            (
                                [0] => Mobile
                                [1] => Bluetooth  Devices
                            )

                        **[category_id] => Array
                            (
                                [0] => 150
                                [1] => 1384
                            )**

                        [category_name] => Array
                            (
                                [0] => iphone 4s
                                [1] => Bluetooth Speaker
                            )

                    )

                **[media] => Array
                    (
                        [images] => Array
                            (
                                [0] => http://www.myshop.com/file_2111_316.jpg
                                [1] => http://www.myshop.com/file_2112_260.jpg**

                            )

                    )

How can i write a foreach loop inside another foreach loop so that I can obtain all values including [category_id] and [media][images] values.

This is to insert values in 2 separate tables.

My current code is:

foreach($new_items as $key => $value) {
    $stmt = $conn->prepare("INSERT INTO   products(id,weight,price,cb_url,product_name) 
                            VALUES(:id,:weight,:price,:url,:name)");
    $stmt->execute(array(':id' => $value['id'], ':weight' => $value['weight'], ':price' => $value['Price'], ':url' => $value['url'],
                  ':name' => $value['name']));

    //second table insert
    $PID = $conn->lastInsertId();
    $stmt = $conn->prepare("INSERT INTO temp_products(productid,catid) 
                            VALUES(:pid,:catid)");
    $stmt->execute(array(':pid' => $PID, ':catid' => $value['category']['category_id']));
}

Everything is going fine, except that the catid column in table 2 is getting the value "array" inserted in it, where there are multiple items in the [category_id] array element above. If only 1 item is present it inserts correctly. I need to insert [media][images] similarly.

Should we need to write a foreach loop inside the existing foreach loop? Or else how to proceed? Help sought...

Since category_id is an array, you need another foreach loop:

//second table insert
$PID = $conn->lastInsertId();
$stmt2 = $conn->prepare("INSERT INTO temp_products(productid, catid) VALUES(:pid, :catid)");
if (!is_array($value['category']['category_id'])) {
    $value['category']['category_id'] = array($value['category']['category_id']);
}
foreach ($value['category']['category_id'] as $catid) {
    $stmt2->execute(array(':pid' => $PID, ':catid' => $catid));
};

Notice, by the way, that you only need to prepare a statement once, outside the loop. You should do the same thing with the first INSERT statement.

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