1

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.

dmotors
  • 611
  • 2
  • 7
  • 19
  • this may help you http://stackoverflow.com/questions/22001121/php-filter-array-values-and-remove-duplicates-from-multi-dimensional-array – A l w a y s S u n n y Apr 01 '15 at 17:02
  • That's good pseudocode: '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'. Try writing the php. To make things easier, start off with a smaller array, like with three car-values or something. Then scale up to match your full problem. – Hektor Apr 01 '15 at 17:03
  • personally I would hash the items you want together ( or unique ) using sha1, and then use that hash as a top level key. Then just build a new array hashing them and the uniqueness of the keys in php will take care of the rest. – ArtisticPhoenix Apr 01 '15 at 17:07
  • @BeingSunny Thanks for that link. Although that link was meant for a single matching key/value I was able to modify it for my situation. I will post the answer. – dmotors Apr 01 '15 at 17:41

2 Answers2

1

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] => 
        )

)
user789456
  • 173
  • 6
  • Note that you may have a potential bug in there. What if you have a car with `'make' => 'Hon'` and `'model' => 'daCivic'`. I admit, in this case it isn't very likely that will ever happen, but you get the point and it is worth mentioning I believe. I would probably at least separate the values in that key with a unlikely string like `%|%` or something, just to make sure. +1 though, cause I believe this is basically the way to go. – Pevara Apr 01 '15 at 22:08
  • Would it be as simple as `$sorted_array["$v[makes]$s$v[models]$s$v[years]$s$v[trims]$s$v[engines]"]=$v;` whereas `$s = "%|%"` ? – dmotors Apr 02 '15 at 00:30
  • And thanks for the answer @user789456 by the way it's similar but more elegant. – dmotors Apr 02 '15 at 00:38
  • Hi dmotors, thanks, here is night 2.41 AM. I will try for you tomorrow one more time. :) – user789456 Apr 02 '15 at 00:42
0

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);
Community
  • 1
  • 1
dmotors
  • 611
  • 2
  • 7
  • 19