1
Good I get the following JSON via post:
string '{
"pedidos": [{
"feito_data": "2017-08-07",
"valor": 40.0
}]
}' (length=265)
With php I handle the data like this:
// Recebe dados do JSON
$mix = filter_input(INPUT_POST, 'json', FILTER_DEFAULT);
// Decodifica json
$jsonObj = json_decode($mix);
// Faz o parsing da string, criando o array
$mix = $jsonObj->pedidos;
// Navega pelos elementos do array, e cadastra novos clientes
foreach ($mix as $c) {
echo $c->feito_data;
}
Well what I want to do is this, as json will only send me 1 result I don’t need to use the foreach
and I don’t need to name the object as pedidos
.
My question is how to receive the data so $c->feito_data
?
Did a var_dump on $here I put the post with json to make sure the data came as you’re imagining ?
– Otto
I will now edit the question ok.
– Hugo Borges
I’m sure the
$aqui
It’s coming bad, I took the test now here, and everything looks right.– 13dev
Syntax is correct, but really the return of $here must be wrong.
– Otto
I don’t understand the problem, if you’re gonna use the
foreach
just oneecho $c
to print the value.– rray
What is the return of
var_dump($mix)
?– Marcelo de Andrade
How do I not need to put a name on the son? like
$mix = $jsonObj->pedidos;
– Hugo Borges
@Marcelodeandrade already added the return in the question.
– Hugo Borges
You want to catch only the first occurrence of
feito_data
? For your keypedidos
is an array.– Marcelo de Andrade
That’s right my key
pedidos
is an array, but will always contain only 1 result, I want to get the value without needing theforeach
– Hugo Borges
Just take it this way: $feitoData = $jsonObj->pedidos[0]->feito_data or change your json so that it always returns an object without being inside the array.
– Diego Schmidt
@Diegoschmidt vlw misses exactly what I wanted.
– Hugo Borges