multidimensional functions concatenar combine array_merge_recursive array php arrays merge

concatenar - php array functions



php | Array merge (1)

$result = array(); foreach ($input as $subarray) { $id = $subarray[''id'']; if (isset($result[$id])) { // Same ID $result[$id][''qty''] += $subarray[''qty'']; // Add quantities } else { $result[$id] = $subarray; // New ID, put in results } } $result = array_values($result); // Convert from associative array to indexed

Tengo una matriz:

Array ( [0] => Array ( [qty] => 2 [id] => 1 [name] => Name 1 [sku] => Model 1 [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. [price] => 209.00 ) [1] => Array ( [qty] => 1 [id] => 1 [name] => Name 1 [sku] => Model 1 [options] => Color: <em>Black (+10$)</em>. Memory: <em>16GB</em>. [price] => 110.00 ) [2] => Array ( [qty] => 1 [id] => 3 [name] => Name 2 [sku] => Model 2 [options] => [price] => 100.00 ) )

El primer paso es encontrar la misma identificación. Y si existe la misma identificación, convierta una matriz. ¿Es posible obtener una matriz de salida (si id es la misma, eliminar una y agregar cantidad a la otra)?

Array ( [0] => Array ( [qty] => 3 // 2+1 [id] => 1 [name] => Name 1 [sku] => Model 1 [options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>. [price] => 209.00 ) [1] => Array ( [qty] => 1 [id] => 3 [name] => Name 2 [sku] => Model 2 [options] => [price] => 100.00 ) )

¡Gracias!