2017-09-27 73 views
-2

我在PHP一個這樣的數組: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元件gudangbarang,涉及具有元件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' 
    ] 
] 

我需要刪除gudangblok,然後CONCAT每個lokasi_terakhir元素。 請指教。

+1

https://stackoverflow.com/help/how-to-請問 – Calimero

+0

問題不清,投票關閉 – RomanPerekhrest

回答

0

請嘗試

$gudang = $array['gudang']; 
$blok = $array['blok']; 
foreach($array as $key =>$val){ 
    $array['lokasi_terakhir'] = $gudang.$blok.$val['lokasi_terakhir']; 
} 
print_r($array); 
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); 
+0

PHP警告'yii \ base \ ErrorException'帶有消息'Illegal string offset'lokasi_terakhir'' –

+0

從輸入數組中刪除'gudang'和'blok'。 –

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); 
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); 
相關問題