I found no question about filtering and ordering the output of a rewritten JSON.
There is no such thing as filtering JSON, you write already filtering the object, it is you who is controlling the object before it is JSON, transforming into JSON is the last step, what matters is to understand what you want and make use of the basic language, learn the basics, I’ll put parts to understand better:
delete / ignore those that have no date, ie dateEnvio = null?
But that’s the minimum basic principle of programming, use if ()
, plain as that:
foreach ($objcont->content as $content) { //Transforma o JSON decodificado em Array
if ($content->dataEnvio) {
$item = array(
"tiporemessa" => $content->tipoRemessa->nome,
"periodo" => $content->periodo->nome,
"exercicio" => $content->historicoRemessa->exercicio,
"dataenvio" => $content->dataEnvio,
"arquivo" => "http://app.tce.ma.gov.br:8889/remessas/download/" . $content->arquivo
);
$output[] = $item;
}
}
And how to filter all elements that contain the object ( typeRemessa->name ) with the value
If it is you who controls the JSON that will still be generated is if
, is the basic, is the minimum, if you expect an exact value, then it would be within the foreach
:
if ($tipoRemessa->nome == 'RGF - LEGISLATIVO MUNICIPAL') {
...
}
Since there are two needs, you have to use the &&
to consider both conditions:
foreach ($objcont->content as $content) { //Transforma o JSON decodificado em Array
if ($content->dataEnvio && $tipoRemessa->nome == 'RGF - LEGISLATIVO MUNICIPAL') {
$item = array(
"tiporemessa" => $content->tipoRemessa->nome,
"periodo" => $content->periodo->nome,
"exercicio" => $content->historicoRemessa->exercicio,
"dataenvio" => $content->dataEnvio,
"arquivo" => "http://app.tce.ma.gov.br:8889/remessas/download/" . $content->arquivo
);
$output[] = $item;
}
}
Finally, to sort you have the documentation of PHP itself for arrays (because as I said you treat the object, make into JSON is after that):
I I cannot answer about sort, because at no point in the question did you say what that sort should be like, unless you’re confusing sorting with filtering, anyway, you don’t sort or filter JSON, you do it with the object (the array), because you control it, and for this you can use this function (which is in the documentation):
I do not want to criticize or give you personal guesses, but I do not understand, this is the basic programming and you are a member of the site for 5 years, how can you skip the basic steps? All of this is learned in the beginning, focus on learning the basics of the new beginning, because this will help you avoid problems and even exaggerated codes. You may think it will take time, but there’s no way forward without the basics.
And when
dataEnvio
fornull
? :)– Luiz Felipe
@Luizfelipe then its respective element is ignored / deleted from the list as those who are
null
do not serve. =)– Alexandre Lopes
I think it’s worth researching about the function
usort
PHP. Also, regarding the empty dates, just don’t do the push ($output[] = $item
) to the new array if the date isnull
(and any other criteria you wish).– Luiz Felipe
@Luizfelipe can turn your comment into a reply?
– Alexandre Lopes