1

I really need your help.

I know how to create with PHP and SQL a json array.

I am creating mine

But I need to create a nested one, and I kinda got lost there.

I saw many other questions similar to this one but still cant get it.

I want my array to looks like this one:

"names": {
   "name_id": {
       "firstname": "john",
       "lastname": "doe",
       "age": 18,
       "menu": {
           "dinner": {
               "dinner1": "119",
               "dinner2": "229",
               "dinner3": "379",
               "dinner4": "559"
           }
        },
        "deep_link_url": "http:\/\/testurl.com\/?name_id=42422&ref=CC",
        "bonus_offers": "some_bonus"
   },

Here is my code:

$return_arr = array();

$sql = "sp_getdata";
$fetch = sqlsrv_query($connection,$sql); 
while ($row = sqlsrv_fetch_array($fetch)) {
   $row_array['name_id'] = $row['name_id'];
   $row_array['firstname'] = $row['firstname'];
   $row_array['lastname'] = $row['lastname'];
   $row_array['age'] = $row['age'];
   $row_array['dinner1'] = $row['dinner1'];
   $row_array['dinner2'] = $row['dinner2'];
   $row_array['dinner3'] = $row['dinner3'];
   $row_array['dinner4'] = $row['dinner14'];
   $row_array['deep_link_url'] = url().'/name_id='.$name_id;
   $row_array['bonus_offers'] = $row['bonus_offer'];

   array_push($return_arr,$row_array);
}

echo json_encode($return_arr);

I get everything into just one. Whats the way to make them nested?

Sorry if my question is not clear enough for someone but that's how I am imagine that in my head. Please someone explain and help!

5
  • your $row_array should be multidimensional array Commented Nov 9, 2017 at 10:57
  • @geeth So should I just change ti to "$row_array['menu']['dinner']['dinner1']" or nothing like that? Commented Nov 9, 2017 at 10:58
  • @geeth if not can you please let me know how? Commented Nov 9, 2017 at 10:58
  • What about RTFM? php.net/manual/en/language.types.array.php sec 14 Commented Nov 9, 2017 at 10:59
  • @AlivetoDie why you dont? It does Commented Nov 9, 2017 at 11:00

1 Answer 1

1

You need to do it like below:-

$return_arr = array();

$sql = "sp_getdata";
$fetch = sqlsrv_query($connection,$sql); 
while ($row = sqlsrv_fetch_array($fetch)) {
   $row_array['name_id'] = $row['name_id'];
   $row_array['firstname'] = $row['firstname'];
   $row_array['lastname'] = $row['lastname'];
   $row_array['age'] = $row['age'];
   $row_array['menu']['dinner']['dinner1'] = $row['dinner1']; //check change
   $row_array['menu']['dinner']['dinner2'] = $row['dinner2']; //check change
   $row_array['menu']['dinner']['dinner3'] = $row['dinner3']; //check change
   $row_array['menu']['dinner']['dinner4'] = $row['dinner14']; //check change
   $row_array['deep_link_url'] = url().'/name_id='.$name_id;
   $row_array['bonus_offers'] = $row['bonus_offer'];

   $return_arr["names"][] = ["name_id"=>$row_array]; // instead of push assign directly
}

echo json_encode($return_arr);

A demo output:- https://eval.in/896214

Sign up to request clarification or add additional context in comments.

2 Comments

Is not 100% what I want to display but thanks for give me the idea of how to solve it. Really appreciate it. Just have to make a bit changes but that make all this json array much clearer.. Thanks
@Maria glad to help you :):)

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.