0

I want a json with this format :

{"dernierNumeroDEVIS":[{"numero_devis":"48"}]}

But I have this :

{"dernierNumeroDEVIS":{"numero_devis":"48"}}

My PHP :

    $array = [
        "numero_devis" => "0"
    ];
    $arrayDevis = array ('dernierNumeroDEVIS' => $array );
    echo json_encode($arrayDevis);
2
  • 1
    array ('dernierNumeroDEVIS' => $array ) -> array ('dernierNumeroDEVIS' => [$array] ) Commented Aug 23, 2016 at 15:51
  • 1
    Break out the Garlic, all the vampires are decending on this question Commented Aug 23, 2016 at 15:56

6 Answers 6

1

You need to wrap your array in an array

$array = [
    "numero_devis" => "0"
];
$arrayDevis = array ('dernierNumeroDEVIS' => [$array]);
echo json_encode($arrayDevis);

To avoid confusion perhaps it'll be easier to understand if you used standard objects and arrays in PHP as they will be the same when formatted as JSON.

$obj = new \stdClass();
$obj->numero_devis = 0;
$obj2 = new \stdClass();
$obj2->dernierNumeroDEVIS = [
    $obj,
];
echo json_encode($obj2);
Sign up to request clarification or add additional context in comments.

1 Comment

I do like a person thats not afraid of stdClass() and does not try to do everything with arrays UV
0

I hope you need two dimensional array

$array = array("numero_devis" => "0");
$arrayDevis = array ('dernierNumeroDEVIS' => array($array) );
echo json_encode($arrayDevis);

Comments

0
$array = [];
$array['dernierNumeroDEVIS'][] = ['numero_devis'=>48];
echo json_encode($array);

Output:

{"dernierNumeroDEVIS":[{"numero_devis":"48"}]}

Comments

0

Not sure why you need it this way but here you go ...

$array = [
    array(
        "numero_devis" => "0",
     )
];
$arrayDevis = array ('dernierNumeroDEVIS' => $array );
echo json_encode($arrayDevis);

Result:

{"dernierNumeroDEVIS":[{"numero_devis":"0"}]}

Comments

0

Actually there is an array inside the array something like that:

$array = array(array("numero_devis"=>"0"));             
$arrayDevis = array('dernierNumeroDEVIS' => $array );
echo json_encode($arrayDevis);

Output:

{"dernierNumeroDEVIS":[{"numero_devis":"0"}]}

Comments

0

In case anyone else hasn't already said, you need to wrap your array inside another array, like this:

array ('dernierNumeroDEVIS' => [$array] )

Heh.

1 Comment

Hahahah Love a sence of humour

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.