我从API获得了以下JSON响应
[
[
{"companyID":"U72200TG2005PTC045191","companyName":"OPEN TEXT TECHNOLOGIES INDIA PRIVATE LIMITED"},
{"companyID":"U51909HR2002PTC034956","companyName":"OPEN WAYS MARKETING PRIVATE LIMITED"},
{"companyID":"U00892HR2005PTC035863","companyName":"OPEN HOUSE DIGITAL MEDIA PRIVATE LIMITED"},
{"companyID":"U00804KA1999PTC024813","companyName":"OPEN LEAF STOCKS AND SHARES PRIVATE LIMITED"},
{"companyID":"U72300KA2000PTC026648","companyName":"OPEN STREAM TECHNOLOGIES (INDIA) PRIVATE LIMITED"},
{"companyID":"U72200KA2003PTC032219","companyName":"OPENCLOVIS SOLUTIONS PRIVATE LIMITED"},
{"companyID":"U72200KA2003PTC032083","companyName":"OPEN-SILICON RESEARCH PRIVATE LIMITED"},
{"companyID":"U72900KA2003PTC033042","companyName":"OPENGEAR NETWORKS PRIVATE LIMITED"},
{"companyID":"U72200DL2005PTC170412","companyName":"OPEN SOLUTIONS SOFTWARE SERVICES PRIVATE LIMITED"},
{"companyID":"U72112MP1985PTC002837","companyName":"OPEN END SPINNERS PVT LTD"}
]
]
我想将其转换为适当的对象数组
[
{"companyID":"U72200TG2005PTC045191","companyName":"OPEN TEXT TECHNOLOGIES INDIA PRIVATE LIMITED"},
{"companyID":"U51909HR2002PTC034956","companyName":"OPEN WAYS MARKETING PRIVATE LIMITED"},
{"companyID":"U00892HR2005PTC035863","companyName":"OPEN HOUSE DIGITAL MEDIA PRIVATE LIMITED"}
]
但我无法实现这个
这是我试过的代码
$data = json_decode($result,true);
$new_array = array();
foreach ($data[0] as $to_obj)
{
new_array[] = (object)$to_obj;
}
return ($new_array);
$ result是来自API的JSON。当我这样做时,$ new_array返回了
array:10 [
0 => {#546
+"companyID": "U72200TG2005PTC045191"
+"companyName": "OPEN TEXT TECHNOLOGIES INDIA PRIVATE LIMITED"
}
1 => {#576
+"companyID": "U51909HR2002PTC034956"
+"companyName": "OPEN WAYS MARKETING PRIVATE LIMITED"
}
2 => {#574
+"companyID": "U00892HR2005PTC035863"
+"companyName": "OPEN HOUSE DIGITAL MEDIA PRIVATE LIMITED"
}
3 => {#552
+"companyID": "U00804KA1999PTC024813"
+"companyName": "OPEN LEAF STOCKS AND SHARES PRIVATE LIMITED"
}
4 => {#548
+"companyID": "U72300KA2000PTC026648"
+"companyName": "OPEN STREAM TECHNOLOGIES (INDIA) PRIVATE LIMITED"
}
5 => {#572
+"companyID": "U72200KA2003PTC032219"
+"companyName": "OPENCLOVIS SOLUTIONS PRIVATE LIMITED"
}
6 => {#571
+"companyID": "U72200KA2003PTC032083"
+"companyName": "OPEN-SILICON RESEARCH PRIVATE LIMITED"
}
7 => {#570
+"companyID": "U72900KA2003PTC033042"
+"companyName": "OPENGEAR NETWORKS PRIVATE LIMITED"
}
8 => {#569
+"companyID": "U72200DL2005PTC170412"
+"companyName": "OPEN SOLUTIONS SOFTWARE SERVICES PRIVATE LIMITED"
}
9 => {#560
+"companyID": "U72112MP1985PTC002837"
+"companyName": "OPEN END SPINNERS PVT LTD"
}
]
但阵列索引也即将到来。我希望它像下面的
[
{"companyID":"U72200TG2005PTC045191","companyName":"OPEN TEXT TECHNOLOGIES INDIA PRIVATE LIMITED"},
{"companyID":"U51909HR2002PTC034956","companyName":"OPEN WAYS MARKETING PRIVATE LIMITED"},
{"companyID":"U00892HR2005PTC035863","companyName":"OPEN HOUSE DIGITAL MEDIA PRIVATE LIMITED"}
]
我如何实现这一目标?我已经尝试了但是我的最终JSON响应包含了我不想要的数组索引。
答案 0 :(得分:0)
就个人而言,我会以这种方式绕过迭代:
$data = (array)json_decode($result);
return reset($data);
这将保留您的内部值作为对象,并仍然将内部值作为对象返回。
如果必须进行迭代,则应将array_values
称为返回:
// Everything that you have above, just replace the return with
return array_values($new_array);
这将消除键,它将使json_encode
将它视为数组,而不是专门的对象。
也就是说,如果您正在执行var_dump
或var_export
,它将始终包含索引号。这只是格式化的一部分。