簡體   English   中英

如何在PHP中的現有json對象中添加動態鍵值對

[英]How to add dynamic key value pair in existing json object in php

我想知道如何在執行期間在對象的Json數組中添加動態鍵/值對。

$result2來自數據庫查詢結果

while( $row = mysql_fetch_assoc($result2) )
{
 $result_show[]=$row;
 $result_show[]['ismy']='1';
 $result_show[]['time']='close';
 $result_show[]['img']  = $path;
}

我的結果是:

{"results":[{"id":"412"},{"ismy":"0"},{"time":"close"},{"img":"1.JPG"}]}

期望的結果是:

{"results":[{"id":"412","ismy":"0","time":"close","img":"1.JPG"}]}

這怎么了 請幫幫我。

使用此代碼:-

<?php
$i = 0;
while( $row = mysql_fetch_assoc($result2) ) {
{
    $result_show[$i]=$row;
    $result_show[$i]['ismy']='1';
    $result_show[$i]['time']='close';
    $result_show[$i]['img']  = $path;
     $i++;
}
?>

問題是您正在使用縮短版的array_push函數四次。 您真正想要做的是將一個元素添加到結果數組,嘗試使用:

while( $row = mysql_fetch_assoc($result2) ) {
  $row['ismy']   = '1';
  $row['time']   = 'close';
  $row['img']    = $path;
  $result_show[] = $row;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM