[英]Merging/grouping multidimensional arrays
我目前有兩個看起來像這樣的數組,我一直在嘗試分組/合並,但是沒有運氣。
$array1;
Array
(
[1] => Array
(
[options] => 1
[barcode] =>
[supplier] => 10
[topcat] => Fibre,1
[cat] => Acrylic,1
[range] => Clearance Chenille,14
[colour] =>
[type] => Ball
[option] => Array
(
[1] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
)
)
[2] => Array
(
[options] => 1
[barcode] =>
[supplier] => 10
[topcat] => Fibre,1
[cat] => Acrylic,1
[range] => Clearance Chenille,14
[colour] =>
[type] => Ball
[option] => Array
(
[1] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
[2] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
)
)
)
$array2;
Array
(
[1] => Array
(
[1] => Array
(
[0] => Array
(
[name] => 500aqua.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C70.tmp
[error] => 0
[size] => 133659
)
)
)
[2] => Array
(
[1] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C71.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C72.tmp
[error] => 0
[size] => 211420
)
)
[2] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C83.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C84.tmp
[error] => 0
[size] => 211420
)
)
)
)
我一直試圖像這樣合並它們:
Array
(
[1] => Array
(
[options] => 1
[barcode] =>
[supplier] => 10
[topcat] => Fibre,1
[cat] => Acrylic,1
[range] => Clearance Chenille,14
[colour] =>
[type] => Ball
[option] => Array
(
[1] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
[files] => Array
(
[0] => Array
(
[name] => 500aqua.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C70.tmp
[error] => 0
[size] => 133659
)
)
)
)
)
[2] => Array
(
[options] => 1
[barcode] =>
[supplier] => 10
[topcat] => Fibre,1
[cat] => Acrylic,1
[range] => Clearance Chenille,14
[colour] =>
[type] => Ball
[option] => Array
(
[1] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
[files] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C71.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C72.tmp
[error] => 0
[size] => 211420
)
)
)
[2] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
[files] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C83.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php6C84.tmp
[error] => 0
[size] => 211420
)
)
)
)
)
)
目前得到:
Array
(
[0] => Array
(
[0] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
[files] => Array
(
[1] => Array
(
[0] => Array
(
[name] => 500aqua.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php1534.tmp
[error] => 0
[size] => 133659
)
)
)
)
[1] => Array
(
[0] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
[1] => Array
(
[type] => Ball
[barcode] =>
[length] =>
[wpi] =>
[dyeable] =>
[feltable] =>
[sold_weight] =>
[gauge] =>
[price] =>
[pack_qty] =>
[shipping_weight] =>
[cost_price] =>
)
[files] => Array
(
[1] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php1535.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php1545.tmp
[error] => 0
[size] => 211420
)
)
[2] => Array
(
[0] => Array
(
[name] => 500beige.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php1546.tmp
[error] => 0
[size] => 148940
)
[1] => Array
(
[name] => 500beige-zoom.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php1547.tmp
[error] => 0
[size] => 211420
)
)
)
)
)
到目前為止,我已經嘗試過:
$group = array();
foreach($array1 as $key1 => $val1){
$group[] = array_merge($val1, array("files" => $array2[$key1]));
}
我尚未對其進行測試(可能會在一個小時左右的時間里進行測試),但是我認為這可能會有所幫助:
foreach($array1 as $key => $val){
foreach($val['option'] as $k_inner => $v_inner){
$array1[$key]['option'][$k_inner]['files'] = $array2[$key][$k_inner];
}
}
您將擁有array2作為您的$group
數組。 一旦測試,我將對其進行編輯。
編輯:更正的算法。 錯誤的原因在於我混淆了$ array1和$ array2,並且我沒有考慮正確的深度和鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.