0

I must send some parameters with api as json format and there is sample for one of parametes like below:

[
 [
   {"PassengerAge":20,"Gender":false}
 ]
]

Now i create json but how can i put it on two array( [[]] ) that be like up?

$rooms = json_encode(["PassengerAge"=>20,"Gender"=>false]);

Now $rooms export is:

{"PassengerAge":20,"Gender":false}

so how can i convert $rooms to example in laravel?

4
  • try using $rooms->toArray() Commented Sep 9, 2020 at 9:34
  • 1
    @AnkitJindal how can you call a method on a string? Commented Sep 9, 2020 at 9:39
  • As per your code it is shown as an object {"PassengerAge":20,"Gender":false} Commented Sep 9, 2020 at 9:40
  • @AnkitJindal json_encode returns a string, that is how the string looks when you output it Commented Sep 9, 2020 at 9:47

1 Answer 1

1

In order to get output like the example above, you can wrap this json in as many arrays as you want:

$rooms = json_encode([[["PassengerAge"=>20,"Gender"=>false]]]);

This will give the following output:

"[[{"PassengerAge":20,"Gender":false}]]"

When you decode this:

json_decode($rooms)

You get the following structure:

  [
     [
       {#4768
         +"PassengerAge": 20,
         +"Gender": false,
       },
     ],
   ]

Tested this in tinker

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.