php - 如何将结果值映射到laravel中的键值?
问题描述
我有 2 个数组,第一个是数据,第二个是结果数组
我正在尝试将结果数组与数据数组映射,所以我使用了 combine() 方法
但问题是,如果我有相同的结果,那么它会映射 1 个项目并跳过剩余的考虑
第一个数组($customersidurl)
[
19 => null,
20 => null,
21 => null,
24 => "31.4346084,74.2793016,12",
25 => null,
26 => "31.58834,74.37375"
]
第二个结果数组($shortest)
[
0 => 8532.8587780495,
1 => 8532.8587780495,
2 => 8532.8587780495,
3 => 18.831126689097,
4 => 8532.8587780495,
5 => 0.85071506409078
]
我的输出是
[
"" => 8532.8587780495,
31.4346084,74.2793016,12 => 18.831126689097,
31.58834,74.37375 => 0.85071506409078,
]
它跳过了 3 个结果。我不希望这种跳过发生。我使用的代码是
$customersidurl = Customer::whereIn('created_by', $adminot)
->get()
->pluck('location_url', 'id');
foreach ($customersidurl as $short) {
$shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
}
$combined = $customersidurl->combine($shortest);
有人可以帮助我任何其他可以将每个元素映射到相应的元素吗?
所需的输出是
[
"" => 8532.8587780495,
"" => 8532.8587780495,
"" => 8532.8587780495,
31.4346084,74.2793016,12 => 18.831126689097,
"" => 8532.8587780495,
31.58834,74.37375 => 0.85071506409078
]
解决方案
在传递 ID 时试试这个
$customersidurl = Customer::whereIn('created_by', $adminot)->get()->pluck('location_url', 'id');
foreach ($customersidurl as $short){
$shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
}
$customersid = Customer::whereIn('created_by', $adminot)->pluck('id')->toArray();
for($i = 0; $i < sizeof($customersid); $i++){
$maping[$customersid[$i]] = $shortest[$i];
}
推荐阅读
- solr - 更改设置后登录管理 UI 不会进入
- locking - glibc 中的flock() 与 fcntl() 语义
- winforms - WinForms中用户控件的属性注入
- javascript - 如何取消订阅 mobx-state-tree 中的 onSnaphot()?
- python - 使用 matplotlib plt.barh 时,invert_xaxis 会出错
- sql - Oracle SQL:在选择查询中需要具有默认十进制值的浮点列
- matlab - 如何为 8 个可能的参数生成所有可能的序列
- python - 如何绘制密度直方图而不是计数?(Matplotlib)
- python - 使用额外的 pip install 将 Python 应用程序部署到 Heroku
- amazon-s3 - 将 5GB 及以上的大文件从 Google Cloud Storage Bucket 复制到 S3 Bucket