将两个json数组合并到PHP中

时间:2015-01-26 06:06:47

标签: php mysql arrays json

// view.php

include("include/dbconnection.php");
dbconnect();

$var    = array();

if(isset($_GET['access_token']))
{
    $access =   $_GET['access_token'];

    $query1 =   "SELECT * FROM db_gcm WHERE access_token='$access'";
    $result1=   mysql_query($query1) or die(mysql_error());
    $rows1  =   mysql_fetch_array($result1);
    $usernam=   $rows1['username'];

    $sql    = "SELECT * FROM db_post ORDER BY id DESC LIMIT 10";
    $result = mysql_query($sql);

    while($obj = mysql_fetch_object($result)) 
    {
        $var[] = $obj;
    }
    echo '{"posts":'.json_encode($var).'}';

    $query2 =   "SELECT * FROM db_post ORDER BY id DESC LIMIT 10";
    $result2=   mysql_query($query2);
    $length2=   mysql_num_rows($result2);
    while($rows2    =   mysql_fetch_array($result2))
    {
        $id[]   =   $rows2['id'];
    }

    for($i=0;$i<$length2;$i++)
    {
        $query3 =   "SELECT * FROM db_like WHERE post_id='$id[$i]' AND access_token='$usernam'";
        $result3=   mysql_query($query3);
        $length3=   mysql_num_rows($result3);
        if($length3>0)
        {
            $arr    =   array('Like'=>"Liked");
            echo '{"Likes":'.json_encode($arr).'}';     
        }
        else
        {
            $arr    =   array('Like'=>"Not Liked");
            echo '{"Likes":'.json_encode($arr).'}';
        }
    }
}

我的json输出

{"posts":[{"id":"3","name":"fgd","username":"fgdff","unique_key":"fgdf","pub":"hjty","pub_date":"jty","post":"hyjty","post_time":"gedth","post_date":"yhjnty","photo":"yjyt","publish":"1"},{"id":"2","name":"sdfgs","username":"sdfg","unique_key":"rfger","pub":"rgter","pub_date":"rtger","post":"rtge","post_time":"regte","post_date":"rthge","photo":"reg","publish":"1"},{"id":"1","name":"","username":"11111","unique_key":"","pub":"asdeqwe","pub_date":"12\/12\/12","post":"dsfwadf","post_time":"08:45 AM","post_date":"03\/12\/2014","photo":"","publish":"1"}]}{"Likes":{"Like":"Liked"}}{"Likes":{"Like":"Not Liked"}}{"Likes":{"Like":"Liked"}}

从上面的json输出我想在“Posts”JSON数组中打印“Likes”JSON数组。有什么方法吗?我尝试了很多但没有结果。请帮助。

1 个答案:

答案 0 :(得分:1)

试试这个。 这不是JSON问题。

include("include/dbconnection.php");
dbconnect();

$var = array();

if(isset($_GET['access_token']))
{
    $access = $_GET['access_token'];

    $query1  ="SELECT * FROM db_gcm WHERE access_token='$access'";
    $result1 = mysql_query($query1) or die(mysql_error());
    $rows1   = mysql_fetch_array($result1);
    $usernam = $rows1['username'];

    $sql    = "SELECT * FROM db_post ORDER BY id DESC LIMIT 10";
    $result = mysql_query($sql);
    while($arr = mysql_fetch_array($result)) 
    {
        // like or not
        $query3  = "SELECT * FROM db_like WHERE post_id='$arr['id']' AND access_token='$usernam'";
        $result3 = mysql_query($query3);
        $length3 = mysql_num_rows($result3);
        $arr['Like'] = $length3 ? 'Liked' : 'Not Liked';

        $var[] = $arr;

    }
    echo '{"posts":'.json_encode($var).'}';

}