Reputation: 631
Let's say I have an array that looks like this:
Array
(
[0] => Array
(
[id] => 44091
[epid] => 109912002
[makes] => Honda
[models] => Civic
[years] => 2000
[trims] => All
[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] => All
[engines] => 1.6L 1595CC l4 GAS DOHC Naturally Aspirated
[notes] =>
)
[2] => Array
(
[id] => 151086
[epid] => 109956658
[makes] => Honda
[models] => Civic
[years] => 1999
[trims] => All
[engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
[notes] =>
)
)
And I would like to somehow merge/group/combine whatever you call it if specific key/value pairs are matching.
So my condition would be:
If Makes & Models & Years & Trims is the same, combine into 1 array. The other key/values such as id/epid/trims/engines/notes are not relevant and if possible can just use/inherit 1 of those matched entries.
Once that's possible I want to add another condition to also look for this:
If Makes & Models & Years & Trims & Engines is the same combine into 1 array.
Perhaps I'm confusing myself and both those can be using the same code.
Anyways in this situation I would expect the outcome to look like this afterwards:
Array
(
[0] => Array
(
[id] => 44091
[epid] => 109912002
[makes] => Honda
[models] => Civic
[years] => 2000
[trims] => All
[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] => All
[engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
[notes] =>
)
)
Notice the array with the years of 1999 was not merged.
I tried messing with array_unique, array_flip but couldn't get it to work.
If it matters I'm using PHP 5.6.7.
Hope someone knows what I'm talking about.
Thanks.
Upvotes: 1
Views: 350
Reputation: 173
This could be helpful
echo '<pre>';
foreach($name_of_your_array as $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] => All
[engines] => 1.6L 1595CC l4 GAS DOHC Naturally Aspirated
[notes] =>
)
[1] => Array
(
[id] => 151086
[epid] => 109956658
[makes] => Honda
[models] => Civic
[years] => 1999
[trims] => All
[engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
[notes] =>
)
)
Upvotes: 1
Reputation: 631
Using Being Sunny's suggestion for this link:
php filter array values and remove duplicates from multi dimensional array
I was able to modify that since that was only meant for a single key/value and it is now working using this:
// Create dummy array for checking duplicates
$taken = array();
// Loop through each item and if doesn't exist add to taken array. If exist then unset the key.
foreach($comps as $key => $item) {
$string = $item['makes'] . $item['models'] . $item['years'] . $item['trims'] . $item['engines'];
if(!in_array($string, $taken)) {
$taken[] = $string;
} else {
unset($comps[$key]);
}
}
// Reindex the array
$comps = array_values($comps);
Upvotes: 0