Inserting JSON object array in mysql with PHP

Asked

Viewed 402 times

0

Good afternoon, you guys. First forgive me if it’s a very ridiculous question, I’m learning to tinker with the language still. I have the following json:

[
{
    "id": 0,
    "codigo": "847010",
    "valorTotal": 3652.44,
    "qtdTotal": 20,
    "descricao": "Lorem ipsum dolor sit amet,",
    "produtos": [
        {
            "id": 0,
            "nome": "Produto1",
            "preco": 100,
            "quantidade": 50,
            "totalParcial": 1826.22
        },
        {
            "id": 1,
            "preco": 100,
            "quantidade": 50,
            "totalParcial": 1826.22
        }
    ]
},
{
    "id": 1,
    "codigo": "010410",
    "valorTotal": 408.84000000000003,
    "qtdTotal": 6,
    "descricao": "Lorem ipsum dolor sit amet,",
    "produtos": [
        {
            "id": 2,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        },
        {
            "id": 3,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        },
        {
            "id": 4,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        }
    ]
}

I would like to know how to go through this array and insert it into mysql with php, I am using json_decode to transform JSON into array and doing a foreach to traverse it-Ló, I would need the products to be inserted with the code that precedes them, Each product is a ROW in the products table. Could someone give me an example, a light, please, I’m banging my head with this.

  • It seems to be on the right track - or at least functional. Inside the loop foreach you can run the SQL that inserts the data into the database. What data set is using?

1 answer

2


You can use the json_decode to convert JSON to Array.

Example:

$json = '[{
    "id": 0,
    "codigo": "847010",
    "valorTotal": 3652.44,
    "qtdTotal": 20,
    "descricao": "Lorem ipsum dolor sit amet,",
    "produtos": [{
            "id": 0,
            "nome": "Produto1",
            "preco": 100,
            "quantidade": 50,
            "totalParcial": 1826.22
        },
        {
            "id": 1,
            "preco": 100,
            "quantidade": 50,
            "totalParcial": 1826.22
        }
    ]
}, {
    "id": 1,
    "codigo": "010410",
    "valorTotal": 408.84000000000003,
    "qtdTotal": 6,
    "descricao": "Lorem ipsum dolor sit amet,",
    "produtos": [{
            "id": 2,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        },
        {
            "id": 3,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        },
        {
            "id": 4,
            "preco": 10,
            "quantidade": 2,
            "totalParcial": 136.28
        }
    ]
}]';

$json_para_array = json_decode($json, TRUE);

echo '<pre>'; // defini preformatacao
print_r($json_para_array); // imprime

Exit:

Array
(
    [0] => Array
        (
            [id] => 0
            [codigo] => 847010
            [valorTotal] => 3652.44
            [qtdTotal] => 20
            [descricao] => Lorem ipsum dolor sit amet,
            [produtos] => Array
                (
                    [0] => Array
                        (
                            [id] => 0
                            [nome] => Produto1
                            [preco] => 100
                            [quantidade] => 50
                            [totalParcial] => 1826.22
                        )

                    [1] => Array
                        (
                            [id] => 1
                            [preco] => 100
                            [quantidade] => 50
                            [totalParcial] => 1826.22
                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [codigo] => 010410
            [valorTotal] => 408.84
            [qtdTotal] => 6
            [descricao] => Lorem ipsum dolor sit amet,
            [produtos] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [preco] => 10
                            [quantidade] => 2
                            [totalParcial] => 136.28
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [preco] => 10
                            [quantidade] => 2
                            [totalParcial] => 136.28
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [preco] => 10
                            [quantidade] => 2
                            [totalParcial] => 136.28
                        )

                )

        )

)

To "work" the array, use a loop.

Example:

foreach($json_para_array as $v) {

    echo 'id: ' . $v['id'] . ' codigo: ' . $v['codigo'] . '<br>';
}

Exit:

id: 0 codigo: 847010
id: 1 codigo: 010410

With these examples, you will practically have to change inside your loop, to generate queries and insertion in the bank.

If you have doubts how to generate queries, insert, etc, then it would be another question, if it would not be too wide, and would probably be closed.

Test this basic part, test out how to make a simple insert, then just add the 2!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.