php - Merging combining arrays if certain key/values matches in an array -
let's have array looks this:
array ( [0] => array ( [id] => 44091 [epid] => 109912002 [makes] => honda [models] => civic [years] => 2000 [trims] => [engines] => 1.6l 1590cc 97cu. in. l4 gas sohc naturally aspirated [notes] => ) [1] => array ( [id] => 77532 [epid] => 83253884 [makes] => honda [models] => civic [years] => 2000 [trims] => [engines] => 1.6l 1595cc l4 gas dohc naturally aspirated [notes] => ) [2] => array ( [id] => 151086 [epid] => 109956658 [makes] => honda [models] => civic [years] => 1999 [trims] => [engines] => 1.6l 1590cc 97cu. in. l4 gas sohc naturally aspirated [notes] => ) )
and somehow merge/group/combine whatever call if specific key/value pairs matching.
so condition be:
if makes & models & years & trims same, combine 1 array. other key/values such id/epid/trims/engines/notes not relevant , if possible can use/inherit 1 of matched entries.
once that's possible want add condition this:
if makes & models & years & trims & engines same combine 1 array.
perhaps i'm confusing myself , both can using same code.
anyways in situation expect outcome afterwards:
array ( [0] => array ( [id] => 44091 [epid] => 109912002 [makes] => honda [models] => civic [years] => 2000 [trims] => [engines] => 1.6l 1590cc 97cu. in. l4 gas sohc naturally aspirated [notes] => ) [1] => array ( [id] => 151086 [epid] => 109956658 [makes] => honda [models] => civic [years] => 1999 [trims] => [engines] => 1.6l 1590cc 97cu. in. l4 gas sohc naturally aspirated [notes] => ) )
notice array years of 1999 not merged.
i tried messing array_unique, array_flip couldn't work.
if matters i'm using php 5.6.7.
hope knows i'm talking about.
thanks.
this helpful
echo '<pre>'; foreach($name_of_your_array $k=>$v){ $sorted_array["$v[makes]$v[models]$v[years]$v[trims]"]=$v; } $sorted_array=array_values($sorted_array); print_r($sorted_array);
output:
array( [0] => array ( [id] => 77532 [epid] => 83253884 [makes] => honda [models] => civic [years] => 2000 [trims] => [engines] => 1.6l 1595cc l4 gas dohc naturally aspirated [notes] => ) [1] => array ( [id] => 151086 [epid] => 109956658 [makes] => honda [models] => civic [years] => 1999 [trims] => [engines] => 1.6l 1590cc 97cu. in. l4 gas sohc naturally aspirated [notes] => ) )
Comments
Post a Comment