omukiguy
omukiguy

Reputation: 1437

Merge inner PHP arrays into one multidimensional array - No duplicates basing on Key value

I have tried a number of PHP functions like array_unique to merge the arrays I have but that does not work since these are not strings.

The starting output would be this on var_dump( $country_cities );

array (size=3)
  0 => 
    array (size=1)
      'BH' => 
        array (size=4)
          'post_id' => int 7886
          'country' => string 'BH' (length=2)
          'city_name_eng' => string 'Laurence' (length=8)
          'city_name_arabic' => string '3684hdfpfwbhisf' (length=15)
  1 => 
    array (size=1)
      'BH' => 
        array (size=4)
          'post_id' => int 7885
          'country' => string 'BH' (length=2)
          'city_name_eng' => string 'Bahrain City' (length=12)
          'city_name_arabic' => string 'vgdg824762' (length=10)
  2 => 
    array (size=2)
      'BH' => 
        array (size=4)
          'post_id' => int 7885
          'country' => string 'BH' (length=2)
          'city_name_eng' => string 'Bahrain City' (length=12)
          'city_name_arabic' => string 'vgdg824762' (length=10)
      'KW' => 
        array (size=4)
          'post_id' => int 7841
          'country' => string 'KW' (length=2)
          'city_name_eng' => string 'Kuwait City' (length=11)
          'city_name_arabic' => string ' مدينة الكويت' (length=24)

The Code below merges the different arrays above.

<?php

    // Make the cities unique. Remove duplicates form the inner array.
    $uniques = [];

    foreach($country_cities as $arr ) {
        foreach( $arr as $v) {
            if( !in_array($v, $uniques, false) ) {
                $uniques[$v['country']] = $v;
            }
        }
    }

var_dump($uniques);

Results logged show the code cuts out some of the arrays.

/Users/..... on line php:74:

array (size=2)
  'BH' => 
    array (size=4)
      'post_id' => int 7885
      'country' => string 'BH' (length=2)
      'city_name_eng' => string 'Bahrain City' (length=12)
      'city_name_arabic' => string 'vgdg824762' (length=10)
  'KW' => 
    array (size=4)
      'post_id' => int 7841
      'country' => string 'KW' (length=2)
      'city_name_eng' => string 'Kuwait City' (length=11)
      'city_name_arabic' => string ' مدينة الكويت' (length=24)

Please help figure out my error or suggest a better way to fix it.

Upvotes: 0

Views: 83

Answers (2)

Remy
Remy

Reputation: 807

If I understood you correctly, this should be the result you wanted to achieve. Each country code will be included once, while the cities (based on post_id) will only be added once.

<?php

$result = [];
$processedIds = [];

foreach ($country_cities as $ccs) {
    foreach ($ccs as $cc => $details) {
        // If the country has not yet been added to
        // the result we create an outer array.
        if (!key_exists($cc, $result)) {
            $result[$cc] = [];
        }

        $postId = $details['post_id'];
        if (!in_array($postId, $processedIds)) {
            // Add the unique city to country's collection
            $result[$cc][] = $details;

            // Keep track of the post_id key.
            $processedIds[] = $postId;
        }
    }
}

print_r($result);

Upvotes: 1

dekameron
dekameron

Reputation: 76

<?php

//...
$uniques = [];

    foreach($country_cities as $data ) {
        foreach( $data as $countryCode => $item) {
            if( !array_key_exists($countryCode, $uniques) ) {
                $uniques[$countryCode] = []; //Create an empty array 
            }
            $targetArray = &$uniques[$countryCode];
            if( !array_key_exists($item['post_id'], $targetArray) ) {
                $targetArray[$item['post_id']] = $item; //Create an empty array 
            }
        }
    }

Maybe this code will help?

Upvotes: 1

Related Questions