0

I have two different queries which I have to append in same array in form of JSON..

Here is my code from 1st query...

while($row = mysqli_fetch_array($res)) {
    array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}

Here is my second query push similar as first one.. number of rows is always same as above query

array_push($result,array('status'=>'$status');

After that I'm encoding them like this

echo json_encode(array("result"=>$result));

Here is what I am getting

{"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26"},
          {"status":"status"}]

But I want to result like this

 {"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26","status":"status"}]

I mean status will merge into my every node... how can I achieve this..?

9
  • user array_merge insted of array_push like this array_merge($result,array('status'=>'$status'); Commented Jan 12, 2017 at 5:46
  • @JYoThI not worked after doing this it was added like this.... {"result":{"0":{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26"},"status":"status"}} Commented Jan 12, 2017 at 5:48
  • try my updated comment Commented Jan 12, 2017 at 5:49
  • @JYoThI status gone right now... {"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26"}]} Commented Jan 12, 2017 at 5:52
  • do a $result['status'] = $status; then do your array_push Commented Jan 12, 2017 at 5:53

2 Answers 2

2

Try the below to add status field to each array:

while($row = mysqli_fetch_array($res)){
      array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}

$res_row = 0;
while($row2 = mysqli_fetch_array($res2)){
   $status = $row2[0]; // status value here
   $result[$res_row]['status']=$status;
   $res_row++;
}

echo json_encode(array("result"=>$result));
Sign up to request clarification or add additional context in comments.

5 Comments

a littile bit issue with it ... a status null is adding at start... all nodes are assigned a name like 0,1,2
I am not clear with your issue. can you please explain more... @Abhishek
i am getting this... {"result":{"status":null,"0":{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26","status":"P"}}}
all status are correct but a status was adding at start
sorry it was my mistake of adding a null status.. thanks for you help
1

Try this please, using temporary arrays that are merged after all your queries are complete:

// Query 1
while($row = mysqli_fetch_array($res)){
$tmpResults[] = $row;
}

// Query 2
$tmpResult2 = array('status'=>'$status');

// Merge Everything
$final = array_merge($tmpResults, $tmpResult2);

// Encode
$json = json_encode($final, TRUE);

Good luck

1 Comment

Thanks for you help

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.