2017-09-15 115 views
2

我試圖生成以下JSON數據:從PHP foreach循環看起來像這樣內PHP JSON編碼

[ 
    { 
     "ID": "A1000", 
     "name": "John Simpson", 
     "serialID": "S41234", 
     "tagID": "ABC6456" 
    }, 
    { 
     "ID": "B2000", 
     "name": "Sally Smith", 
     "serialID": "B5134536", 
     "tagID": "KJJ451345" 
    }, 
    { 
     "ID": "A9854", 
     "name": "Henry Jones", 
     "serialID": "KY4239582309", 
     "tagID": "HYG452435" 
    } 
] 

foreach($records as $record) { 

     $ID = $record->getField('propertyID'); 
     $name = $record->getField('assignedName'); 
     $serialID = $record->getField('serial'); 
     $tagID = $record->getField('tag'); 

    } 

我已經嘗試使用:

$arr = array('ID' => $ID, 'name' => $name, 'serialID' => $serialID, 'tagID' => $tagID); 

它適用於循環內的每個記錄,但不能計算出連接的語法t一起擺成一個單一的JSON數據字符串?

+2

不會覆蓋'$ arr'每次迭代中,只需添加'[]' – Ghost

回答

4

問題: -你有過寫你$arr變量中foreach()每個時間loop.That導致問題。

解決方案: -,而不是通過寫作,賦值數組象下面這樣: -

$arr = []; //create empty array 

foreach($records as $record) { 

    $arr[] = array(
       'ID' => $record->getField('propertyID'); 
       'name' => $record->getField('assignedName'); 
       'serialID' => $record->getField('serial'); 
       'tagID' => $record->getField('tag') 
      );//assign each sub-array to the newly created array 
} 
echo json_encode($arr); 
+0

精確,很好的解決方案.. +1 –

+0

@SahilGulati感謝。欣賞它。 –

0

記錄添加到某種聚集陣列,然後json_encode它:

$items = []; 
foreach($records as $record) { 
    $items[] = [ 
     'ID' => $record->getField('propertyID'), 
     'name' = $record->getField('assignedName'), 
     'serialID' => $record->getField('serial'), 
     'tagID' => $record->getField('tag'), 
    ]; 
} 

echo json_encode($items); 
0

試試這個:

$data= array(); 
foreach($records as $record) { 

$data[] = array(
     'ID' => $record->getField('propertyID'); 
     'name' => $record->getField('assignedName'); 
     'serialID' => $record->getField('serial'); 
     'tagID' => $record->getField('tag') 
     ); 
    } 

echo json_encode($data); 

說明:製造陣列,把所有的數值放在它們的數字索引之一的循環中,json_enccode()它。

0

您可以使用類似array_map()此:

$result = array_map(function ($record) { 
    return [ 
     'ID'  => $record->getField('propertyID'), 
     'name'  => $record->getField('assignedName'), 
     'serialID' => $record->getField('serial'), 
     'tagID' => $record->getField('tag'), 
    ]; 
}, $records); 

echo json_encode($result); 

希望這有助於!

0
<?php 
$ArrayName = array(); 

foreach($records as $record) { 

     $ID = $record->getField('propertyID'); 
     $name = $record->getField('assignedName'); 
     $serialID = $record->getField('serial'); 
     $tagID = $record->getField('tag'); 

$ArrayName[] = array('ID' => $ID, 'name' => $name, 'serialID' => $serialID, 'tagID' => $tagID); //assign each sub-array to the newly created array 

    } 
echo json_encode($ArrayName); 

? 

> Blockquote 
0

這個代碼將幫助您:

<?php 
$resultArray = array(); 

foreach($records as $record) 
{ 
     $newResultArrayItem = array(); 
     $newResultArrayItem['ID'] = $record->getField('propertyID'); 
     $newResultArrayItem['name'] = $record->getField('assignedName'); 
     $newResultArrayItem['serialID'] = $record->getField('serial'); 
     $newResultArrayItem['tagID'] = $record->getField('tag'); 

     $resultArray[] = $newResultArrayItem; 
} 

$encodedArray = json_encode($ArrayName); 

echo $encodedArray; 

?>