laravel - 如何排列数组的数据?
问题描述
我有这个 json_decode 数组:
"[{"pracetamol":"cabsol","bandol":"bottol"},{"2":"77","4":"99"}]"
dd 为他们:
我需要像这样安排它们:
pracetamol - cabsol - 2 - 77
bandol - bottol - 4 - 99
我使用了这段代码,但不像我需要的那样工作:
$decoded = json_decode($doctor->pharmacys, true);
@foreach($decoded as $d)
@foreach($d as $k => $v)
{{"$k - $v\n"}} <br>
@endforeach
@endforeach
解决方案
您可以使用此代码更好地排列数据:
$decoded = json_decode($doctor->pharmacys, true);
$result = [];
foreach($j as $k1 => $v1){
$i=0;
foreach($v1 as $k2 => $v2){
isset($result[$i]) ? array_push($result[$i],$k2,$v2) : $result[$i] = [$k2,$v2];
$i++;
}
}
结果:
Array
(
[0] => Array
(
[0] => pracetamol
[1] => cabsol
[2] => 2
[3] => 77
)
[1] => Array
(
[0] => bandol
[1] => bottol
[2] => 4
[3] => 99
)
)
在巴德:
@foreach($result as $d)
@foreach($d as $v)
{{$v}} @if(!$loop->last) - @endif
@endforeach
@if(!$loop->last) <br> @endif
@endforeach
推荐阅读
- mongodb - 与 minikube 相比,在云中运行时 Pod 不通信
- javascript - 嵌套 JS 同一个提供者的两个实例
- java - 尝试在 Java 中连接到 postgres db 时出现 org.postgresql.util.PSQLException
- gcc - 禁用动态内存分配?
- anova - anova_lm() 中 typ 的使用
- php - require_images 的值是多少
- javascript - vuejs - how to add custom icons to Quill (undo/redo)
- javascript - 为什么我可以通过一些 NodeList 而不是所有的 forEach?
- php - Laravel 按值求和
- visual-studio-mac - 每次我尝试登录时,Visual Studio for Mac 都会崩溃