-1

I have JSON like this (from nested sorting)

[
 {"id":13},{"id":14},
 {"id":15,
   "children":[
          {"id":16},{"id":17},{"id":18}
    ]
 },
 {"id":19},{"id":20},
 {"id":21,
      "children":[
          {"id":22}
      ]
 }
]

how I PHP loop to put this JSON in MySQL

Thank you.

aldrin27
  • 3,407
  • 3
  • 29
  • 43
ku_thai
  • 43
  • 6

3 Answers3

0

As with any valid JSON format string, you can use PHP's built-in json_decode to convert it into a parsable object and then loop through those parameters. In this case, from the JSON string you've given (which seems to be an array)

$array = json_decode($string);
foreach ($array as $val) {
    //The object $val will be:
    "id":13
}

If it's nested, you would do another foreach loop and detect for the property that needs to be looped. For example you could do this in a variety of ways (check for the "children" property, loop through the $val properties and check if it is an array, if it is then loop through that). Inside this foreach loop iteration, you can insert it, or do execute whatever statement you need to get it inside MySQL

Your question is pretty vague on the format and way you want it inserted. I'd suggest showing some code you've tried so other people can help you and know what direction you're going in. (that's probably the reason for the downvote, not mine by the way)

Looping through all the properties of object php

Community
  • 1
  • 1
q.Then
  • 2,743
  • 1
  • 21
  • 31
  • I think he wants to store his data inside mysql. Not get access to it. – aldrin27 Sep 07 '15 at 03:03
  • I edited my answer to show him that he can access the data and use it iteration in `foreach` to insert it (overhead costs, but I'm not sure what he wants to do with it). Thanks for the feedback! – q.Then Sep 07 '15 at 03:05
0

just put your valid json inside json_decode and assign it to a php array as below

//$php_arr = json_decode('YOUR_JSON');
$php_arr = json_decode('[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]');

/*comment the following 3 lines when done*/
echo "<pre>";
print_r($php_arr);
echo "</pre>";
/*comment the above 3 lines when done*/

output

Array
(
    [0] => stdClass Object
        (
            [id] => 13
        )

    [1] => stdClass Object
        (
            [id] => 14
        )

    [2] => stdClass Object
        (
            [id] => 15
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 16
                        )

                    [1] => stdClass Object
                        (
                            [id] => 17
                        )

                    [2] => stdClass Object
                        (
                            [id] => 18
                        )

                )

        )

    [3] => stdClass Object
        (
            [id] => 19
        )

    [4] => stdClass Object
        (
            [id] => 20
        )

    [5] => stdClass Object
        (
            [id] => 21
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 22
                        )

                )

        )

)

Now as you have PHP array do what ever you want like

 foreach($php_arr as $arr){
    if(!isset($arr['children'])){
        $q = "insert into tbl(id) values('".$arr['id']."')";
    }else{
        //your logic
    }
 }
alamnaryab
  • 1,480
  • 3
  • 19
  • 31
0

You need to use json_decode function to decode JSON data. Then use foreach loop to manipulate data.

Try example

$str = '
    [{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]
';

$json = json_decode($str);

//var_dump($json);
foreach ($json as $item)
{
    //The object $val will be:
    echo $item->id."<br />";
    //You INSERT query is here
    //echo "INSERT INTO table (field) VALUE ($item->id)";
    $query = mysqli_query($conn, "INSERT INTO table (field) VALUE ($val->id)");
}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50