2016-10-12 6 views
1

Ich möchte ein unterordnetes Array aus einem mehrdimensionalen Array entfernen, falls ein doppelter Wert für einen bestimmten Schlüssel gefunden wird. Die answer(s) here hat überhaupt nicht funktioniert. Die answer here funktioniert jedoch für eine große Anzahl von Arrays, die ziemlich langsam wird. Auf der Suche nach einer saubereren und schnelleren Lösung.Entfernen Sie Child Array, wenn Wert eines Schlüssels Duplicate ist

Beispiel 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']; 
    } 

Und das print_r($sections) Ergebnis.

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 
     ) 

) 

Beide Array[0] und Array[3] haben den gleichen Wert für den Schlüssel id, dafür die gesamten Array[3] in meinem Fall entfernt werden muss, um Duplikate zu vermeiden.

Dies funktioniert für mich, aber es wird sehr langsam, wenn es 100s oder mehr Arrays gibt.

$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); 

Versuchte mehrere Antworten hier in Stackoverflow (einschließlich this), aber keiner von ihnen in meinem Fall geholfen.

Dank

+0

Mögliche Duplikat von [Wie doppelte Werte aus einem mehrdimensionalen entfernen Array in PHP] (http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Epodax

Antwort

1

Sie die ganze array_column und array_filter Verwendung ändern können -

//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); 

Für PHP < 5,5

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

$idArray = array_unique(array_map(function($section){return $section['id'];}, $section)); 
+0

Schwerwiegender Fehler: Schwerwiegender Fehler: Aufruf von undefinierter Funktion array_column() in /XXXX/XXXX/XXX/editor.php. Ich verwende PHP 5.4.45 – Abhik

+0

Es wurde in> 5.5 hinzugefügt. besser fortfahren mit foreach oder 'array_map' – jitendrapurohit

+0

Danke, aber immer noch diesen fatalen Fehler, da 'array_column' immer noch da ist bei' $ idArray = array_unique (array_column ($ section,' id ')); '. – Abhik

Verwandte Themen