Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将新的php数组键传递到json中_Php_Json - Fatal编程技术网

将新的php数组键传递到json中

将新的php数组键传递到json中,php,json,Php,Json,我有2个表用户(id、userid、fn、ln)和userdetails(id、userid、图像、历史、位置、活动) 我已经为第一个表编写了一个查询来检索所有数据,我只需要第二个表中的历史和位置 我已检索到数组,并将其发送到json_encode。 现在我想检索历史记录、位置并创建一个新的键历史记录,我想向历史记录键添加历史记录位置值 我需要一个查询和json格式 对于特定的用户,我需要检索的是自己的历史记录 在这两个表中,用户id是相同的 提前谢谢 $sth=mysql_查询(“从用户中选

我有2个表用户(id、userid、fn、ln)和userdetails(id、userid、图像、历史、位置、活动) 我已经为第一个表编写了一个查询来检索所有数据,我只需要第二个表中的历史和位置

我已检索到数组,并将其发送到json_encode。 现在我想检索历史记录、位置并创建一个新的键历史记录,我想向历史记录键添加历史记录位置值

我需要一个查询和json格式

  • 对于特定的用户,我需要检索的是自己的历史记录 在这两个表中,用户id是相同的 提前谢谢

    $sth=mysql_查询(“从用户中选择*)$结果=mysql\u fetch\u assoc($sth); $i=0; foreach($result as$data){ $final_数组[$i]['id']=$data['id']; $final_数组[$i]['email']=$data['email']; $final_数组[$i]['fname']=$data['fname']; $final_数组[$i]['lname']=$data['lname']

        $sth2 = mysql_query("SELECT id,places,act FROM user_dates WHERE user_id= '".$data['email']."'");
        $result2 = mysql_fetch_assoc($sth2);
        $j=0;
        $history_array = array();
        foreach($result2 as $data2) { 
          $history_array[$j] = array("id" => $data2['id'],"places" => $data2['places'], "act " => $data2['act ']);
          $j++;
        }
        $final_array[$i]['history'] = $history_array;
    
    
        $i++;
    } 
    echo json_encode($final_array);
    
[ { “id”:“81”, “用户id”:“2011年”, “fn”:“asd”, “ln”:“wer”, “历史”:[ { “id”:“350”, “历史”:“make1”, “位置”:“qwe” } ] }, { “id”:“82”, “用户id”:“2012”, “fn”:“asd1”, “ln”:“wer1”, “历史”:[ { “id”:“350”, “历史”:“make2”, “位置”:“qwe2” } ] }
]

Userdetails
表包含每个用户的多条记录。因此,您需要进行子查询并获得结果,然后形成一个
ground维数组
。最后,
编码为JSON

$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
$result = $sth->fetchAll();

$i=0;
foreach($result as $data) { 
    $final_array[$i]['id'] = $data['id'];
    $final_array[$i]['userid'] = $data['userid'];
    $final_array[$i]['fn'] = $data['fn'];
    $final_array[$i]['ln'] = $data['ln'];

    $sth2 = $dbh->prepare("SELECT location,activity FROM userdetails WHERE userid= ".$data['id']."");
    $sth2->execute();
    $result2 = $sth2->fetchAll();
    $j=0;
    $history_array = array();
    foreach($result2 as $data2) { 
      $history_array[$j] = array("location" => $data2['location'], "activity " => $data2['activity ']);
      $j++;
    }
    $final_array[$i]['history'] = $history_array;


    $i++;
} 
echo json_encode($final_array);

假设如果我想从表1中获取所有数据,即用户,并且只从第二个表2中获取历史和活动,用户详细信息您可以简单地添加“select users.*,userdetails.history,userdetails.ACTIVE”,对于历史,我缺少方括号“history”:[{“id”:“350”,“history”:“make1”,“Location”:“qwe”}]如上所示,可以为这些$final_数组[$i]['history']=array(数组(“id”=>$data['userDetailsId'],“history”=>$data['history'],“Location”=>$data['Location'])做些什么;查询正在运行依赖于2个表如果我想要2运行一个依赖于1个表的查询例如:我只有两个用户,这两个用户将有n个历史记录存储在表2中,我只需要输出中的2行所有历史记录都应该在用户的特定行中。你能告诉我是什么解决了我的问题吗