无法在laravel中将数组数组转换为没有索引的对象数组

Posted

技术标签:

【中文标题】无法在laravel中将数组数组转换为没有索引的对象数组【英文标题】:Unable to convert array of arrays to array of objects without index in laravel 【发布时间】:2018-02-04 03:47:31 【问题描述】:

我有以下从 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 响应包含我不想要的数组索引。

【问题讨论】:

我相信这不是问题,这是 php 数组的行为,如果数组没有任何索引,则默认情况下它将采用数字索引,请查看 3v4l.org/FkPPS @Ajeesh 我认为$data = json_decode($result,true); 和使用$data[0] 会给你预期的输出。 【参考方案1】:

就个人而言,我会以这种方式绕过迭代:

$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_dumpvar_export,它将始终包含索引号。这只是格式的一部分。

【讨论】:

以上是关于无法在laravel中将数组数组转换为没有索引的对象数组的主要内容,如果未能解决你的问题,请参考以下文章

在 Laravel 中将二维数组转换为 json 对象

在laravel中将对象转换为数组[重复]

在laravel中将对象转换为数组[重复]

在laravel 5.1中将对象转换为数组

如何在laravel中将两级数组转换为一个级别数组

在 laravel 控制器中将数组转换为 json