JSON中的PHP对象数组
我正在开发一个API,以便能够使用PHP以JSON格式在数组中提供一些数据JSON中的PHP对象数组,php,arrays,json,Php,Arrays,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']
$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\u FORCE\u对象
并删除加号计数器$++
。它应该能解决你的问题
$resultJSON = json_encode($newArray);
JSON\u FORCE\u对象
使用非关联数组时,输出对象而不是数组。当输出的接收者需要一个对象,而数组为空时,特别有用。从PHP5.3.0开始提供
您只需要像下面这样对数组进行编码,以避免使用数组键
$resultJSON = json_encode(array_values($newArray));
请检查第一个json的输出,它的格式似乎不正确,我该如何检查?我使用$resultJSON=(json_encode($newArray,json_FORCE_OBJECT));Json_编码,所以我不知道还能做什么。抱歉:(发布$resultJSON的值,这样我就可以看到输入是什么以及如何格式化为所需的输出$resultJSON的值是当前输出中列出的第二块代码。要我打印并发送吗?不要使用
JSON\u FORCE\u OBJECT
。只需将第二个参数传递给JSON\u encode()
调用即可。