如何通过PHP循环将JSON放到MySQL中?

时间:2015-09-07 02:54:40

标签: php mysql json

我有这样的JSON(来自嵌套排序)

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

我如何循环将这个JSON放在MySQL中

谢谢。

3 个答案:

答案 0 :(得分:0)

与任何有效的JSON格式字符串一样,您可以使用PHP的内置json_decode将其转换为可解析的对象,然后循环遍历这些参数。在这种情况下,从您给出的JSON字符串(似乎是一个数组)

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

如果它是嵌套的,你会做另一个foreach循环并检测需要循环的属性。例如,您可以通过多种方式执行此操作(检查"children"属性,遍历$val属性并检查它是否为array,如果是,则循环执行)。在这个foreach循环迭代中,您可以插入它,或者执行在MySQL中获取它所需的任何语句

您的问题在格式和插入方式上非常模糊。我建议你展示一些你尝试过的代码,这样其他人就可以帮助你,知道你的方向。(这可能是downvote的原因,而不是我的方式)

Looping through all the properties of object php

答案 1 :(得分:0)

将您的有效json放入json_decode并将其分配给php数组,如下所示

//$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*/

输出

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
                        )

                )

        )

)

现在你已经拥有了PHP数组,你可以做任何想做的事情

 foreach($php_arr as $arr){
    if(!isset($arr['children'])){
        $q = "insert into tbl(id) values('".$arr['id']."')";
    }else{
        //your logic
    }
 }

答案 2 :(得分:0)

您需要使用json_decode函数来解码JSON数据。然后使用foreach循环来操作数据。

试试例

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