1

I want to remove a child array from a multi-dimensional array in case a duplicate value found for a particular key. The answer(s) here didn't work at all. The answer here works, however, for large amount of arrays, that gets pretty slower. Looking for a cleaner and faster solution.

Example PHP Array

    $args = array();

    $args[] = array(
        'section' => array(
            'id' => 'section1',
            'name' => 'Section 1',
        ),
        'name' => 'Shortcode Name',
        'action' => 'shortcodeaction',
        'icon' => 'codeicon',
        'image' => 'codeimage',
    );
    $args[] = array(
        'section' => array(
            'id' => 'section2',
            'name' => 'Section 2',
        ),
        'name' => 'Shortcode2 Name',
        'action' => 'shortcodeaction2',
        'icon' => 'codeicon2',
        'image' => 'codeimage2',
    );
    $args[] = array(
        'section' => array(
            'id' => 'section3',
            'name' => 'Section 3',
        ),
        'name' => 'Shortcode3 Name',
        'action' => 'shortcodeaction3',
        'icon' => 'codeicon3',
        'image' => 'codeimage3',
    );
    $args[] = array(
        'section' => array(
            'id' => 'section1',
            'name' => 'Section 4',
        ),
        'name' => 'Shortcode4 Name',
        'action' => 'shortcodeaction4',
        'icon' => 'codeicon4',
        'image' => 'codeimage4',
    );
    $args[] = array(
        'section' => array(
            'id' => 'section5',
            'name' => 'Section 5',
        ),
        'name' => 'Shortcode5 Name',
        'action' => 'shortcodeaction5',
        'icon' => 'codeicon5',
        'image' => 'codeimage5',
    );

    $sections = array();

    foreach ( $args as $arg ) {
        $sections[] = $arg['section'];
    }  

And, the print_r($sections) result.

Array
(
    [0] => Array
        (
            [id] => section1
            [name] => Section 1
        )

    [1] => Array
        (
            [id] => section2
            [name] => Section 2
        )

    [2] => Array
        (
            [id] => section3
            [name] => Section 3
        )

    [3] => Array
        (
            [id] => section1
            [name] => Section 4
        )

    [4] => Array
        (
            [id] => section5
            [name] => Section 5
        )

)

Both Array[0] and Array[3] has the same value for the key id, therefor the entire Array[3] has to be removed in my case to avoid duplicates.

This is working for me though, but it gets really slow when there are 100s or more arrays.

    $knownIds = array();
    foreach( $sections AS $key=>$item ) {
      if( array_key_exists($item['id'], $knownIds) === true ) {
        unset( $sections[$key] );
      } else {
        $knownIds[$item['id']] = $key;
      }
    }
    $sections = array_values($sections);

Tried several answers here in StackOverflow (including this), but none of them helped in my case.

Thanks

Community
  • 1
  • 1
Abhik
  • 664
  • 3
  • 22
  • 40
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Epodax Oct 12 '16 at 10:39

1 Answers1

1

You can modify the whole using array_column and array_filter -

//get all the sections value
$section = array_column($args, 'section');

//store ids in temp array
$idArray = array_unique(array_column($section, 'id'));
//filter the array having unique id
$uniqueSections = array_filter($section, function ($key, $value) use ($idArray) {
    return in_array($value, array_keys($idArray));
}, ARRAY_FILTER_USE_BOTH);

var_dump($uniqueSections);

For PHP <5.5

$section = array_map(function($args) {
             return $args['section'];
           }, $args);

$idArray = array_unique(array_map(function($section){return $section['id'];}, $section));
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39