将新数组插入php json

时间:2015-04-14 17:25:08

标签: php arrays json

我尝试将新数组值推送到JSON

$var=array("code"=>"100");
$sql = SELECT order as orderID, pub as orderCode from cart
while ($row = mysql_fetch_assoc ($sql) {
$var[] = $row;
}
echo '{"status":'. json_encode($var).'}';

我想将此字符串推送到上面的JSON数组

$string = array("total"=>"3000");

我的脚本显示如下:

{  
"status":{  
   "code":"100",
      "0":{  
            "total":"3000"
          },
      "1":{  
            "orderID":"16",
            "orderCode":"14290290685322"
          },
      "2":{  
            "total":"3000"
          }
      }
   }

我希望var total in this:

**////////blablabla
   "1":{ 
      "orderID":"16",
      "orderCode":"14290290685322",
      "total":"3000" 
      }
*///blablba

1 个答案:

答案 0 :(得分:1)

在编码为JSON之前添加$var[] = array("total"=>"3000");

$var=array("code"=>"100");
$sql = SELECT order as orderID, pub as orderCode from cart
while ($row = mysql_fetch_assoc ($sql) {
  $row['total'] = '3000' ;
  $var[] = $row;
}
echo '{"status":'. json_encode($var).'}';