I
I
iurakuleshov2020-05-17 16:11:24
PHP
iurakuleshov, 2020-05-17 16:11:24

How to write multidimensional array to MySql database?

Good afternoon, please tell me how to write a multidimensional array to the MySql database using PDO.

array(3) 
{     [0]=> array(10){ 
         ["id"]=> string(2) "12"
         ["title"]=> string(10) "Шwqe" 
         ["description"]=> string(22) "вывфвфывфвф" 
         ["image"]=> string(0) "" 
         ["dimensions"]=> string(0) "" 
         ["data"]=> string(10) "2020-05-10"
         ["category_id"]=> string(1) "1"
         ["user_id"]=> string(2) "24" 
         ["amount"]=> string(1) "3"
         ["price"]=> string(2) "59" }
    [1]=> array(10) { 
         ["id"]=> string(2) "13" 
         ["title"]=> string(4) "2222" 
         ["description"]=> string(8) "23123123" 
         ["image"]=> string(14) "zpvmful5ll.jpg" 
         ["dimensions"]=> string(7) "268x249" 
         ["data"]=> string(10) "2020-05-10" 
         ["category_id"]=> string(1) "1" 
         ["user_id"]=> string(2) "24"
         ["amount"]=> string(1) "3" 
         ["price"]=> string(2) "60" } 
    [2]=> array(10) { 
         ["id"]=> string(2) "16" 
         ["title"]=> string(10) "data" 
         ["description"]=> string(4) "1121" 
         ["image"]=> string(14) "xqgdre8ib8.jpg"
         ["dimensions"]=> string(7) "268x249"
         ["data"]=> string(10) "2020-05-11" 
         ["category_id"]=> string(1) "6" 
         ["user_id"]=> string(2) "24"
         ["amount"]=> string(1) "1" 
         ["price"]=> string(2) "19" } }

Here is an example of an array, I think this is done through foreach, but I still don’t understand how to implement it correctly.
Tell me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iurakuleshov, 2020-05-18
@iurakuleshov

If anyone is interested, I solved my question like this

foreach($carts as $cart){
            $data = [
                "title" =>  $cart['title'],
                "description" =>  $cart['description'],
                "image" =>  $cart['image'],
                "dimensions" =>  $cart['dimension'],
                "data"  =>  date('Y-m-d'),
                "category_id" =>  $cart['category_id'],
                "user_id"   =>  $this->auth->getUserId(),
                "amount"   =>  $cart['amount'],
                "price"   =>  $cart['price'],

            ];
            И тут уже само PDO
        }

S
sl0, 2020-05-17
@sl0

If the question is how exactly to stuff the entire array, then use serialize / unserialize or convert to json and save as a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question