解码while循环中获取的值

时间:2015-11-24 05:43:03

标签: php mysql json

我想解码我获取的值。我取得了很好的结论,''到值$album.=$fet_pic['CONTENT_VALUE'].',';我想解码该值并将其发送到jsondata。现在' albpic'返回null。我得到了json值,如图所示。

enter image description here

$alb = array();
$get_alb = mysql_query("select * from album");
while($fet_alb = mysql_fetch_array($get_alb)) {
     $id=$fet_alb['ID'];
     $alid=$fet_alb['CONTENT_VALUE'];
     $albpic=mysql_query("select * from album_details where SUB_ID='$id'");
      $album='';        
       while($fet_pic=mysql_fetch_array($albpic))
       {
         $album.=$fet_pic['CONTENT_VALUE'].',';
       }
      // $album = substr($album,0,-1);
       $alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode($album)));

 }  
echo json_encode($alb);

 $album={"media_type":"image/png","content_type":"alb_detail","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"link.png","large_pic_loc":"link.png","filter_type":"image/png","photodescription":"No Description","pic_id":"zhadb"},{"media_type":"image/png","content_type":"alb_detail","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"link.png","large_pic_loc":"link.png","filter_type":"image/png","photodescription":"No Description","pic_id":"zhadb"},{"media_type":"image/video","content_type":"alb_info","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"http://img.youtube.com/vi/fdgd/default.jpg","large_pic_loc":"http://img.youtube.com/vi/fdgd/hqdefault.jpg","filter_type":"image/video","videoname":"fdgd","photodescription":"dfgdfg","pic_id":"kg5k4"}

2 个答案:

答案 0 :(得分:0)

要使用json_decode(),您传递的参数应采用json_encoded格式。

更改此

$alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode($album)));

$alb[] = array_merge(array('id' => $id),$fet_alb['CONTENT_VALUE'], ,array('albpics'=>$album));

如果$fet_alb['CONTENT_VALUE']这是json编码的值,请使用

$arr = json_decode($fet_alb['CONTENT_VALUE']);
$arr1 = imlpode(",",$arr);// this will give comma separated values.

答案 1 :(得分:0)

您的$album变量未返回正确的json值。

所以,尝试用下面的行代替。希望它对你有用。

 $alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode("[".$album."]",true)));