我想将id值推入array.I想用$ alid推送$ id值。我想合并该值。
我想得到
$alid = {"id":"1638926145", "album_desc":"dfgdgdfg", "content_type":"alb_detail", "website_id":"571710720", "user":"admin@gmail.com", "album_title":"fdgfdgfdg", "album_creation_date":"2015-11-23T05:27:03.806Z"}
while($fet_alb = mysql_fetch_array($get_alb)) {
$id=$fet_alb['ID'];
$alid=$fet_alb['CONTENT_VALUE'];
$alb[]= json_decode($fet_alb['CONTENT_VALUE']);
}
$id=1638926145
$alid={"album_desc":"dfgdgdfg","content_type":"alb_detail","website_id":"571710720","user":"admin@gmail.com","album_title":"fdgfdgfdg","album_creation_date":"2015-11-23T05:27:03.806Z"}
答案 0 :(得分:1)
使用
$data['id']=$fet_alb['ID'];
$data['alid'] = $fet_alb['CONTENT_VALUE'];
$alb[]= json_decode($data);
答案 1 :(得分:1)
尝试
$alb[] = array_merge(array('id' => $id), json_decode($fet_alb['CONTENT_VALUE'], true));
这里我已经将JSON转换为数组,然后创建了另一个具有id
的数组,然后将两个数组合并为一个数组作为输出。
修改强>
$array1 = array('id' => $id); // creating a array with only a ID
$array2 = json_decode($fet_alb['CONTENT_VALUE'], true); // converting JSON to array with content
$alb[] = array_merge($array1, $array2); // Merge both arrays to single array
答案 2 :(得分:0)
试试这个。
$id = $fet_alb['ID']
$alid = json_decode($fet_alb['CONTENT_VALUE']);
$alid['id'] = $id;
// to get JSON string
echo json_encode($alid);
答案 3 :(得分:0)
只需将id
属性或数组元素添加到json_decode
结果的结果中,然后执行json_encode
以编码回json
见下文
while($fet_alb = mysql_fetch_array($get_alb)) {
$id=$fet_alb['ID'];
$alid=$fet_alb['CONTENT_VALUE'];
/* using an object from json_decode*/
$alb= json_decode($fet_alb['CONTENT_VALUE']);
$alb->id = $id; //or directly
$alb->id = $fet_alb['ID'];
/*or using an associative array from json_decode*/
$alb= json_decode($fet_alb['CONTENT_VALUE'],true);
$alb['id'] = $id; //or directly
$alb['id'] = $fet_alb['ID'];
/* construct the final json string */
$jsonString = json_encode($alb);
var_dump($jsonString);
/* will output
string(194) "{
"album_desc":"dfgdgdfg",
"content_type":"alb_detail",
"website_id":"571710720",
"user":"admin@gmail.com",
"album_title":"fdgfdgfdg",
"album_creation_date":"2015-11-23T05:27:03.806Z",
"id":1638926145
}"
*/
}