php - Merging by array value (and doing calculations with the other keys) -
i'm having difficult time finding viable solution problem:
this array have, , want group array product_name
, count quantity. array structure needs remain same.
array( [0] => array ( [product_quantity] => 1 [product_name] => appel ) [1] => array ( [product_quantity] => 1 [product_name] => d-day ) [2] => array ( [product_quantity] => 4 [product_name] => d-day ) [3] => array ( [product_quantity] => 2 [product_name] => d-day ) [4] => array ( [product_quantity] => 1 [product_name] => emiel ) [5] => array ( [product_quantity] => 9 [product_name] => emiel ) [6] => array ( [product_quantity] => 3 [product_name] => estella ) [7] => array ( [product_quantity] => 4 [product_name] => feeke ) [8] => array ( [product_quantity] => 1 [product_name] => feeke ) [9] => array ( [product_quantity] => 7 [product_name] => reset ) [10] => array ( [product_quantity] => 1 [product_name] => reset ))
what need output:
array( [0] => array ( [product_quantity] => 1 [product_name] => appel ) [1] => array ( [product_quantity] => 7 [product_name] => d-day ) [2] => array ( [product_quantity] => 10 [product_name] => emiel ) [3] => array ( [product_quantity] => 3 [product_name] => estella ) [4] => array ( [product_quantity] => 5 [product_name] => feeke ) [5] => array ( [product_quantity] => 8 [product_name] => reset ) )
i love hear advice, or solutions on how tackle this!
the goal can reached in many ways. if aim speed use foreach
iterate on input array, incrementally computing , storing values new array (the body of callback function below).
if aim impress workmates or recruiter knowledge of php use array_reduce()
:
$output = array_values( // don't need keys of generated array array_reduce( // incrementally process $input using callback $input, // $carry partial result // $item processed item function(array $carry, array $item) { // extract product name local variable // we'll use several times below $name = $item['product_name']; // if it's new product initialize entry in output if (! array_key_exists($name, $carry)) { $carry[$name] = array( 'product_quantity' => 0, 'product_name' => $name, ); } // update quantity of group $carry[$name]['product_quantity'] += $item['product_quantity']; // return partial result return $carry; }, // start empty array array() ) );
update
the op asked in comment how reuse callback function , make customizable ("how use parameter make 'product_name'
dynamicaly?").
you can put value ('product_name'
) variable ($key
) outside function , use use
language construct let anonymous function inherit it.
$key = 'product_name'; // values needs in variable $output = array_values( array_reduce( $input, // 'use' keyword lets anonymous function use copy of $key function(array $carry, array $item) use ($key) { $name = $item[$key]; // rest of callback's code comes here unchanged // ... // return partial result return $carry; }, array() ) );
see example #3 on documentation of anonymous functions more examples.
you can store callback function variable. doesn't improve readability of code. however, keep reading, intermediate step, real magic happens below.
$key = 'abc'; $f = function(array $carry, array $item) use ($key) { $name = $item[$key]; // rest of callback's code comes here unchanged // ... // return partial result return $carry; }; $output = array_values(array_reduce($input, $f, array()));
to make reusable need have way generate callbacks different values of $key
. let's write simple function creates parametrized callbacks:
// function creates new callback uses value of argument $key function makefn($key) { return function(array $carry, array $item) use ($key) { $name = $item[$key]; // rest of callback's code comes here unchanged // ... // return partial result return $carry; }; }
now, let's use new function. creates different callback functions can used reduce array using various keys:
$output1 = array_values(array_reduce($input, makefn('product_name'), array())); $output2 = array_values(array_reduce($input, makefn('product_code'), array()));
Comments
Post a Comment