我在php中有一个像这样的数组:
[
'gudang' => 'A'
'blok' => '2'
0 => [
'coil_no' => 'DNA07X1C519081726A'
'urut' => '28204'
'lokasi_terakhir' => '411'
]
1 => [
'coil_no' => 'DNA07X1A518081712A'
'urut' => '28205'
'lokasi_terakhir' => '211'
]
2 => [
'coil_no' => 'DNA07X1A420081707A'
'urut' => '28206'
'lokasi_terakhir' => '311'
]
]
我需要将concat元素gudang
,barang
添加到具有元素lokasi_terakhir
的另一个元素,即元素tipe。我需要这样:
[
0 => [
'coil_no' => 'DNA07X1C519081726A'
'urut' => '28204'
'lokasi_terakhir' => 'A2411'
]
1 => [
'coil_no' => 'DNA07X1A518081712A'
'urut' => '28205'
'lokasi_terakhir' => 'A2211'
]
2 => [
'coil_no' => 'DNA07X1A420081707A'
'urut' => '28206'
'lokasi_terakhir' => 'A2311'
]
]
我需要删除gudang
,blok
,然后连接到每个lokasi_terakhir
元素。
请指教。
答案 0 :(得分:1)
尝试使用array_map
:
$input = [
0 => [
'coil_no' => 'DNA07X1C519081726A'
'urut' => '28204'
'lokasi_terakhir' => '411'
]
1 => [
'coil_no' => 'DNA07X1A518081712A'
'urut' => '28205'
'lokasi_terakhir' => '211'
]
2 => [
'coil_no' => 'DNA07X1A420081707A'
'urut' => '28206'
'lokasi_terakhir' => '311'
]
];
$gudang = 'A';
$blok = 2;
$output = array_map(function(&$value) use ($gudang, $blok) {
$value['lokasi_terakhir'] = $gudang.$blok.$value['lokasi_terakhir'];
}, $input);
答案 1 :(得分:0)
请尝试
$gudang = $array['gudang'];
$blok = $array['blok'];
foreach($array as $key =>$val){
$array['lokasi_terakhir'] = $gudang.$blok.$val['lokasi_terakhir'];
}
print_r($array);
答案 2 :(得分:0)
试试这个:
$new_arr = [];
$str = $arr['gudang'] . $arr['blok'];
for($i = 0; $i < count($arr)-2; $i++) {
$new_arr[$i] = $arr[$i];
$new_arr[$i]['lokasi_terakhir'] = $str.$arr[$i]['lokasi_terakhir'];
}
print_r($new_arr);
答案 3 :(得分:0)
$array1 = [
'gudang' => 'A',
'blok' => '2',
0 => [
'coil_no' => 'DNA07X1C519081726A',
'urut' => '28204',
'lokasi_terakhir' => '411'
],
1 => [
'coil_no' => 'DNA07X1A518081712A',
'urut' => '28205',
'lokasi_terakhir' => '211'
],
2 => [
'coil_no' => 'DNA07X1A420081707A',
'urut' => '28206',
'lokasi_terakhir' => '311'
]
];
$length = count($array1);
$array2 = array_splice($array1, 2, $length);
echo json_encode($array2);