Json对象数组mysql插入

时间:2014-01-03 04:53:17

标签: php json

我有以下json对象:

[Object { name="tag[]", value=""w1","x1","y1","z1""}, Object { name="tag[]", value=""w2","x2","y2","y3""}]

在php中,我可以使用这样的帖子将它们存储在数组变量中:

  $tags[] = $_POST['tags'];
  $postid = 1

我想像这样插入数据:

    Insert Into tagtable  ( postid, w, x, y, z, ,0) values ( $postid, "w1", "x1", "y1", "z1"), ($postid, "w2", "x2", "y2", "z2")


 My problem is getting the comma separated values from the $tags variable.

我正在考虑使用一个迭代$ tags数组大小的for循环。任何人都可以指导我如何构造这个循环,以便我可以获得像

这样的变量
   $alltags = '( $postid, "w1", "x1", "y1", "z1"), ($postid, "w2", "x2", "y2", "z2")';

1 个答案:

答案 0 :(得分:0)

尝试此代码....它取决于标签数组的大小....

  $value="";
for($i=0;$i<sizeof($tags);$i++)
{
     $value .= "($postid,";
    for($j=0;$j<sizeof($tag[$i]);$j++)
    {
       $value .="'$tag[$i][$j]',";
    }
    rtrim($value, ",");
    $value .="),";
}
rtrim($value, ",");
$query = "Insert Into tagtable  ( postid, w, x, y, z, ,0) values".$value;