簡體   English   中英

如何將mysql數組結果轉換為json編碼的新數組

[英]How to convert mysql array result into new array for json encoding

我似乎無法得到我需要的結果。 這是由sql查詢提供的數組:

Array (
 [0] => Array ( 
     [id] => 7
     [description] => Accepted )
 [1] => Array ( 
     [id] => 8
     [description] => Declined )
 [2] => Array (
     [id] => 11 
     [description] => Deferred ) 
     )

以下是我需要如何使用上面列出的對象的格式:

['7'][‘7’] = “Accepted”;
['7'][‘8’] = “Declined ”;
['7'][‘11’] = “Deferred”;

...第一個數組['7']是每個對象所需的附加值。

看起來很簡單,但我創建的foreach語句返回錯誤“不能使用標量值作為數組”

$rows = /*Your Data above*/
$data = array('7'=>array());
foreach($rows as $row)
    $data['7'][$row['id']] = $row['description'];

$jsonData = json_encode($data);

可能會完成

暫無
暫無

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

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