Add name to json Key

Asked

Viewed 76 times

-2

In this my code below it returns me json with the key "services:" however I am not able to put a name "Flight:" in the second key. how can I make this key named

$final['servicos'] = [];
$fli['flight'] = [];

foreach($list as $t) {
 $data = new stdClass;

 $data->id = $t->id;
 $data->cliente = $t->comp_name;
 $data->pax = $t->firstName; 
 $data->phone = $t->phone;
 $data->origin = $t->origemida;
 $data->destiny = $t->destinoida;
 $data->status = $t->tra_status;
 $data->info = $t->comments;

 //voo Flight:
 $fli = new stdClass;
 $fli->flightNumber = $t->voo;
 $fli->flightCompany = $t->cia;
 $fli->flightEnd = $t->cia;
 $fli->flightGate = $t->voo;

array_push($final['servicos'], $data,$fli);

       }

 echo json_encode($final);

Bass follows my json saida

{  
"servicos": [
{
  "id": 507,
  "cliente": "C2RIO OFFICE NITEROI",
  "pax": "Robert",
  "phone": "",
  "origin": "Airport GIG -RJ",
  "destiny": "Hotel Hilon Barra",
  "status": "3",
  "info": ""
},
  {
  "flightNumber": "GOL4568",
  "flightCompany": "GOL",
  "flightEnd": "GOL",
  "flightGate": "GOL4568"
  } ] }

My problem is here I’d have to show up like this Flight: {

   ???????{
  "flightNumber": "GOL4568",
  "flightCompany": "GOL",
  "flightEnd": "GOL",
  "flightGate": "GOL4568"
  } ] }     

2 answers

2


If I understand the question you want to add a pair chave/valor in the JSON representation of the object $data. To do this just create a property and pass the desired value.

<?php
$final = [];

$data = new stdClass;

$data->id = '507';
$data->cliente = 'C2RIO OFFICE NITEROI';
$data->pax = ''; 
$data->phone = 'Robert';
$data->origin = 'Airport GIG -RJ';
$data->destiny = 'Hotel Hilon Barra';
$data->status = '3';
$data->info = '';

//voo Flight:
$fli = new stdClass;
$fli->flightNumber = "GOL4568";
$fli->flightCompany = "GOL";
$fli->flightEnd = "GOL";
$fli->flightGate = "GOL4568";


$final['servicos'][] = $data; // Equivale a array_push($final['servicos'], $data);
$data->Flight = $fli; // Cria o par chave/valor desejado

echo json_encode($final, JSON_PRETTY_PRINT);

Which will result in:

{
    "servicos": {
        "id": "507",
        "cliente": "C2RIO OFFICE NITEROI",
        "pax": "",
        "phone": "Robert",
        "origin": "Airport GIG -RJ",
        "destiny": "Hotel Hilon Barra",
        "status": "3",
        "info": "",
        "Flight": {
            "flightNumber": "GOL4568",
            "flightCompany": "GOL",
            "flightEnd": "GOL",
            "flightGate": "GOL4568"
        }
    }
}

Or else if you want the JSON representation of $final is an array with two items:

<?php
$final = [];

$data = new stdClass;

$data->id = '507';
$data->cliente = 'C2RIO OFFICE NITEROI';
$data->pax = ''; 
$data->phone = 'Robert';
$data->origin = 'Airport GIG -RJ';
$data->destiny = 'Hotel Hilon Barra';
$data->status = '3';
$data->info = '';

//voo Flight:
$fli = new stdClass;
$fli->flightNumber = "GOL4568";
$fli->flightCompany = "GOL";
$fli->flightEnd = "GOL";
$fli->flightGate = "GOL4568";

$final['servicos'][] = $data;
$final['Flight'][] = $fli;    

echo json_encode($final, JSON_PRETTY_PRINT);

Which results in:

{
    "servicos": {
        "id": "507",
        "cliente": "C2RIO OFFICE NITEROI",
        "pax": "",
        "phone": "Robert",
        "origin": "Airport GIG -RJ",
        "destiny": "Hotel Hilon Barra",
        "status": "3",
        "info": ""
    },
    "Flight": {
        "flightNumber": "GOL4568",
        "flightCompany": "GOL",
        "flightEnd": "GOL",
        "flightGate": "GOL4568"
    }
}
  • ok was exactly that.... more is affecting something in the loop. is just making a loop

  • 1

    @Fabiohenrique missed putting a [] when doing the push array.

  • Perfect worked perfectly thank you

-1

 "Flight":[
{
  "flightNumber": "GOL4568",
  "flightCompany": "GOL",
  "flightEnd": "GOL",
  "flightGate": "GOL4568"
  }
 ] }    
  • 1

    Gabriel, your answer may be useful, however I recommend you explain it better. I advise you to do the Tour

  • This is exactly how I wanted... more how to make this exit using my code above

Browser other questions tagged

You are not signed in. Login or sign up in order to post.