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 關注
- 91 瀏覽
添加回答
舉報