2 回答
TA贡献1848条经验 获得超6个赞
在循环中,您需要创建所需值的关联数组,并将其推送到整体结果数组中。在循环结束时,输出该数组的结果:json_encode
$json_strdoc = file_get_contents("https://example.com/arquivojson"); //Get the file
$objdoc = json_decode($json_strdoc); //Decoding JSON
$output = array();
foreach ($objdoc->content as $content) {
$item = array(
"usuarioId" => $content->licitacaoId,
"ano" => $content->anoProcesso,
"entidadeId" => $content->unidade->orgao->ente->enteId,
"nome" => $content->unidade->orgao->ente->nome
);
$output[] = $item;
}
echo json_encode($output);
TA贡献1873条经验 获得超9个赞
创建一个包含所需内容的新数组并调用 。json_encode()
此外,数组位于 中,而不是 。你应该循环它,而不是在循环内部使用。$objdoc->content$objdoc$itemdoc->content
<?php
$json_strdoc = file_get_contents("https://example.com/arquivojson"); //Get the file
$objdoc = json_decode($json_strdoc); //Decoding JSON
$newdoc = array_map(function($itemdoc) {
return ["usuarioId" => $itemdoc->userId,
"ano" => $itemdoc->year,
"entidadeId" => $itemdoc->unity->organ->entity->entityId,
"nome" => $itemdoc->unity->organ->entity->name];
}, $objdoc->content);
echo json_encode($newdoc);
- 2 回答
- 0 关注
- 68 浏览
添加回答
举报