动态创建PHP JSON数组

时间:2016-06-07 07:42:24

标签: php arrays 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?

1 个答案:

答案 0 :(得分:1)

试试这个

$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;