2

I have created a JSON in PHP as follows.

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

The JSON is as follows

 [{"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"}},]

As you can see, the "Regular" tag inside the JSON is repeating. However, i wanted the JSON as follows, with just one "regular" array.

[{"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"}}]

How can i tweak the PHP code to create a dynamic JSON as above?

3
  • i cant find regular index while array creation in php Commented Jun 7, 2016 at 7:51
  • Your second json is not valid. Please validate it and edit your question so we know what format you actually want. Also, where does the regular string coming from? Commented Jun 7, 2016 at 7:51
  • Regular String added Commented Jun 7, 2016 at 8:01

1 Answer 1

1

Try this

$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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.