It’s possible, but it’s weird
What you are trying to do is a JSON object with repeated keys/properties. This is unusual as many JSON libraries will error when reading a JSON with repeated keys, and even if there is no error, they probably won’t be able to read all the data. Using your example of wanting a result:
{"name":"student","name":"barbara_cristina","name":"carolina_deus"}
In doing:
$obj = json_decode( $json );
echo $obj->nome;
You expect me to print out what? aluno
, barbara_cristina
or carolina_deus
?
It is not possible to do with json_encode()
Because json_encode()
does not produce objects with repeated keys. You have to do Gambi in hand. Something like this:
<?php
$result = array( "2" => "aluno" , "8" => "barbara_cristina" , "13" => "carolina_deus" );
$fakeItens = array();
foreach ( $result as $item )
$fakeItens[] = '"nome":' . json_encode( $item );
$fakeJson = "{" . implode( ',' , $fakeItens ) . "}";
echo $fakeJson;
This code will generate correct JSON, even if the item has quotes or special characters.
Instead of putting a fixed header, take out the keys?
It looks like the numeric keys that are bothering you. If the "header" is a fixed thing, how about nay put it, and yes take the numbers?
<?php
$result = array( "2" => "aluno" , "8" => "barbara_cristina" , "13" => "carolina_deus" );
echo json_encode( array_values( $result ) );
Compare the result of the two codes:
{"nome":"aluno","nome":"barbara_cristina","nome":"carolina_deus"}
["aluno","barbara_cristina","carolina_deus"]
Click [Edit] and put the code that generates JSON. What you want is very simple, but we need the code to help better (the part that gets the values, not only the
encode
). In this case, it would be by a name in the objects, not a "header". JSON objects are defined as"nome":valor
. http://json.org/ Probably these numbers are coming from some array, and that’s where we have to adjust the names, before the meeting.– Bacco
Okay, I’ve changed it
– Yuri Rodrigues
draw the json output you expect, so that we do it here.
– Ivan Ferrer
I already edited the question, making it more complete, with the commands I used to generate, as it turns out, and as I want to leave...thanks :d
– Yuri Rodrigues
Improved a lot with its edition, but repeating the names does not give. Each pair needs a unique name. If you want the same name, the format will look different. To create things with the same name, it will look like an array of pairs:
[{"nome":"maria"},{"nome":"pedro"},{"nome":"zuul"}]
, otherwise it is not a valid JSON.– Bacco
you want to change the numeric keys to text, got it. I just don’t understand where this variable came from
$variavel
, would not be the$result
?– Ivan Ferrer
there is no way then?
– Yuri Rodrigues
As designed, can not do with JSON. It could generate as a string, but depends a lot on where you will use. If the party that will receive the data wants a JSON, it has to be as I posted in the comment above, an array of objects:
[{"nome":"maria"},{"nome":"pedro"},{"nome":"zuul"}]
– Bacco
yes, just explain to me what you’re doing... where this variable is coming from
$variavel
. Your code doesn’t seem to be complete.– Ivan Ferrer
Alterei la, where it was $variavel put $result
– Yuri Rodrigues
I just don’t understand why you want it this way, because it’s not a correct json, but... you can do it.
– Ivan Ferrer