簡體   English   中英

JSON中的PHP對象數組

[英]PHP array of objects in JSON

我正在開發一個API,能夠使用PHP以JSON格式提供數組中的一些數據。

$newArray = array();
$i=0;
foreach ($resultJSON as $i => $item)
{
    $newArray['result'][$i]['ISO'] =$item['ISO'];
    $newArray['result'][$i]['Country_Name']= $item['Country_Name'];;

    $newArray['result'][$i]['Sector']['Retail']= (int)$item['Retail'];
    $newArray['result'][$i]['Sector']['Food']= (int)$item['Food'];
    $newArray['result'][$i]['Sector']['Transportation']= (int)$item['Transportation'];
    $newArray['result'][$i]['Sector']['Transportation']= (int)$item['Telecom'];
    $newArray['result'][$i]['Sector']['Household_Goods']= (int)$item['Household_Goods'];
    $newArray['result'][$i]['Sector']['Oil_and_Gas']= (int)$item['Oil_and_Gas'];
    $newArray['result'][$i]['Sector']['Electronics']= (int)$item['Electronics'];
    $newArray['result'][$i]['Sector']['Automotive']= (int)$item['Automotive'];
    $newArray['result'][$i]['Sector']['Chemicals']= (int)$item['Chemicals'];
    $newArray['result'][$i]['Sector']['Technology']= (int)$item['Technology'];
    $newArray['result'][$i]['Sector']['Pharmaceuticals']= (int)$item['Pharmaceuticals'];
    $newArray['result'][$i]['Sector']['Construction']= (int)$item['Construction'];
    $newArray['result'][$i]['Sector']['Machinery_and_Equipment']= (int)$item['Machinery_and_Equipment'];
    $newArray['result'][$i]['Sector']['Metals']= (int)$item['Metals'];
    $newArray['result'][$i]['Sector']['Aeronautics']= (int)$item['Aeronautics'];
    $newArray['result'][$i]['Sector']['Business_services']= (int)$item['Business_services'];
    $newArray['result'][$i]['Sector']['Utilities']= (int)$item['Utilities'];
    $newArray['result'][$i]['Sector']['Personal_and_recreational_goods']= (int)$item['Personal_and_recreational_goods'];
    $newArray['result'][$i]['Sector']['Paper']= (int)$item['Paper'];
    $newArray['result'][$i]['Sector']['Other_services']= (int)$item['Other_services'];

    $newArray['result'][$i]['Current_Average']= (int)$item['Current_Average'];
    $newArray['result'][$i]['Forecast_Average']= (int)$item['Forecast_Average'];

    $i++;
}


$resultJSON = (json_encode($newArray, JSON_FORCE_OBJECT));
header('Content-Type: application/json iHYPERLINK');
echo  $resultJSON;

我正在建設的人說他們寧願擁有一系列物品。

目前輸出是

{ "result": { "0": { "ISO": "US", "Country_Name": "U.S.", "Sector": { "Retail": 21, "Food": 34, "Transportation": 35, "Household_Goods": 39, "Oil_and_Gas": 50, "Electronics": 61, "Automotive": 49, "Chemicals": 51, "Technology": 67, "Pharmaceuticals": 62, "Construction": 54, "Machinery_and_Equipment": 57, "Metals": 39, "Aeronautics": 56, "Business_services": 54, "Utilities": 44, "Personal_and_recreational_goods": 45, "Paper": 33, "Other_services": 41 }, "Current_Average": 49, "Forecast_Average": 50 }, "1": { "ISO": "CA", "Country_Name": "Canada", "Sector": { "Retail": 1, "Food": 1, "Transportation": 40, "Household_Goods": 52, "Oil_and_Gas": 71, "Electronics": 63, "Automotive": 79, "Chemicals": 45, "Technology": 84, "Pharmaceuticals": 89, "Construction": 59, "Machinery_and_Equipment": 64, "Metals": 34, "Aeronautics": 58, "Business_services": 55, "Utilities": 46, "Personal_and_recreational_goods": 80, "Paper": 39, "Other_services": 47 }, "Current_Average": 55, "Forecast_Average": 56 }

不過他們已經要求了

{"result":[{ "ISO": "US", "Country_Name": "U.S.", "Sector": { "Retail": 21, "Food": 34, "Transportation": 35, "Household_Goods": 39, "Oil_and_Gas": 50, "Electronics": 61, "Automotive": 49, "Chemicals": 51, "Technology": 67, "Pharmaceuticals": 62, "Construction": 54, "Machinery_and_Equipment": 57, "Metals": 39, "Aeronautics": 56, "Business_services": 54, "Utilities": 44, "Personal_and_recreational_goods": 45, "Paper": 33, "Other_services": 41 }, "Current_Average": 49, "Forecast_Average": 50 }, { "ISO": "CA", "Country_Name": "Canada", "Sector": { "Retail": 1, "Food": 1, "Transportation": 40, "Household_Goods": 52, "Oil_and_Gas": 71, "Electronics": 63, "Automotive": 79, "Chemicals": 45, "Technology": 84, "Pharmaceuticals": 89, "Construction": 59, "Machinery_and_Equipment": 64, "Metals": 34, "Aeronautics": 58, "Business_services": 55, "Utilities": 46, "Personal_and_recreational_goods": 80, "Paper": 39, "Other_services": 47 }, "Current_Average": 55, "Forecast_Average": 56 }]}

所以他們不希望數字顯示,但我不知道如何做到這一點。

任何幫助,將不勝感激。

刪除標志JSON_FORCE_OBJECT並刪除加號計數器$++ 它應該解決你的問題。

$resultJSON = json_encode($newArray);

JSON_FORCE_OBJECT

使用非關聯數組時,輸出對象而不是數組。 當輸出的接收者期望一個對象並且該數組為空時特別有用。 從PHP 5.3.0開始提供。

你只需要像下面那樣編碼數組以避免數組鍵

$resultJSON = json_encode(array_values($newArray));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM