我有类似的数据:
array:3 [▼
"jne" => "Jalur Nugraha Ekakurir (JNE)"
"pos" => "POS Indonesia (POS)"
"tiki" => "Citra Van Titipan Kilat (TIKI)"
]
我只需要此数组的jne, pos, tiki
部分,但我无法获取它们。我试图像这样映射它们:
$couriers = collect($results)->map(function ($item) {
return ['title' => $item];
})->toArray();
它返回:
array:3 [▼
"jne" => array:1 [▼
"title" => "Jalur Nugraha Ekakurir (JNE)"
]
"pos" => array:1 [▼
"title" => "POS Indonesia (POS)"
]
"tiki" => array:1 [▼
"title" => "Citra Van Titipan Kilat (TIKI)"
]
]
如何在1个数组中获得3个标题的数据,以便可以在视图中循环显示?
$rajaongkir = new Raja\Domestic($key);
$results = $rajaongkir->courier('all');
$couriers = collect($results)->map(function ($item) {
return ['title' => $item];
})->toArray();
dd($couriers);
答案 0 :(得分:0)
您可以使用array_keys()
获取数组的所有键。
$arr = [
'jne' => 'Jalur Nugraha Ekakurir (JNE)',
'pos' => 'POS Indonesia (POS)',
'tiki' => 'Citra Van Titipan Kilat (TIKI)',
];
$keys = array_keys($arr);
print_r($keys);
答案 1 :(得分:0)
您可以使用如下代码段:
$results = [
"jne" => [
"title" => "Jalur Nugraha Ekakurir (JNE)"
],
"pos" => [
"title" => "POS Indonesia (POS)"
],
"tiki" => [
"title" => "Citra Van Titipan Kilat (TIKI)"
],
];
$couriers = array_map(function($title) {
return ['title' => $title];
}, array_keys($results));