2016-06-07 96 views
2

我已經在PHP中創建了一個JSON,如下所示。動態創建PHP JSON數組

$result=mysqli_query($mysqli,"SELECT * FROM service_provide WHERE personal_id='".$personal_id."'") or die(mysqli_error($mysqli); 
    $row = mysqli_fetch_assoc($result); 
while($row = mysqli_fetch_assoc($result)) 
{ 



    $data[] = array('regular'=>array(
    ' fname' => $row['fname'], 
     ' email_id' => $row['email_id'], 
     ' phone_number' => $row['phone_number'], 
     ' state' => $row['state'], 
     ' city' => $row['city'], 
     ' main_id' => $row['main_id'], 
     ' sub_id' => $row['sub_id'], 
     ' service_id' => $row['service_id'], 
     'portfolio1' => $row['portfolio1'], 
     'portfolio2' => $row['portfolio2'], 
     'portfolio3' => $row['portfolio3']) 
); 


    } 
     $json = json_encode($data); 
     echo $json; 

的JSON是如下

[{"regular":{" fname":"47788656"," email_id":"47788656"," phone_number":"47788656"," state":"47788656"," city":"47788656"," main_id":"47788656"," sub_id":"47788656"," service_id":"47788656","portfolio1":"47788656","portfolio2":"47788656","portfolio3":"47788656"}}, 

{"regular":{" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"}}, 

{"regular":{" fname":"9875656"," email_id":"9875656"," phone_number":"9875656"," state":"9875656"," city":"9875656"," main_id":"9875656"," sub_id":"9875656"," service_id":"9875656","portfolio1":"9875656","portfolio2":"9875656","portfolio3":"9875656"}},] 

正如你所看到的,JSON裏面的 「常規」 標籤重複。然而,我想要如下的JSON,只有一個「常規」數組。

[{"regular":{" fname":"47788656"," email_id":"47788656"," phone_number":"47788656"," state":"47788656"," city":"47788656"," main_id":"47788656","sub_id":"47788656","service_id":"47788656","portfolio1":"47788656","portfolio2":"47788656","portfolio3":"47788656"}, 

}, 
{" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"}, 

{" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"}}] 

我該如何調整PHP代碼來創建如上所述的動態JSON?

+0

我不能找到,而數組創建常規索引在PHP –

+0

你的第二個'json'無效。請驗證它並編輯您的問題,以便我們知道您實際需要的格式。另外,'regular'字符串從哪裏來? – xjmdoo

+0

已添加常規字符串 –

回答

0

試試這個

$result=mysqli_query($mysqli,"SELECT * FROM service_provide WHERE personal_id='".$personal_id."'") or die(mysqli_error($mysqli); 
$row = mysqli_fetch_assoc($result); 

while($row = mysqli_fetch_assoc($result)) { 
    $data[] = array(
     'fname' => $row['fname'], 
     'email_id' => $row['email_id'], 
     'phone_number' => $row['phone_number'], 
     'state' => $row['state'], 
     'city' => $row['city'], 
     'main_id' => $row['main_id'], 
     'sub_id' => $row['sub_id'], 
     'service_id' => $row['service_id'], 
     'portfolio1' => $row['portfolio1'], 
     'portfolio2' => $row['portfolio2'], 
     'portfolio3' => $row['portfolio3'], 
    ); 
} 
$result['regular'] = $data; 
$json = json_encode($result); 
echo $json;