1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Remove duplicate entries from multidimensional array

Discussion in 'PHP' started by secretdesign, Jul 21, 2015.

  1. #1
    Hi,

    I'm having trouble to remove duplicate entries from multidimensional array.

    Here is my code:
    <?php var_dump($entries['demo'] ); ?>
    
    array(6) {
      [0]=>
      array(4) {
        ["city"]=>
        string(8) "Brisbane"
        ["course"]=>
        string(19) "Excel 2010 Beginner"
        ["price"]=>
        string(3) "330"
        ["dates"]=>
        array(2) {
          [0]=>
          string(8) "Jul 21st"
          [1]=>
          string(8) "Aug 26th"
        }
      }
      [1]=>
      array(4) {
        ["city"]=>
        string(8) "Brisbane"
        ["course"]=>
        string(23) "Excel 2010 Intermediate"
        ["price"]=>
        string(3) "330"
        ["dates"]=>
        array(2) {
          [0]=>
          string(8) "Jul 22nd"
          [1]=>
          string(8) "Aug 27th"
        }
      }
      [2]=>
      array(4) {
        ["city"]=>
        string(8) "Brisbane"
        ["course"]=>
        string(19) "Excel 2010 Advanced"
        ["price"]=>
        string(3) "530"
        ["dates"]=>
        array(2) {
          [0]=>
          string(8) "Jul 23rd"
          [1]=>
          string(8) "Aug 28th"
        }
      }
      [3]=>
      array(4) {
        ["city"]=>
        string(6) "Sydney"
        ["course"]=>
        string(19) "Excel 2010 Beginner"
        ["price"]=>
        string(3) "330"
        ["dates"]=>
        array(2) {
          [0]=>
          string(8) "Jul 28th"
          [2]=>
          string(8) "Aug 26th"
        }
      }
      [4]=>
      array(4) {
        ["city"]=>
        string(6) "Sydney"
        ["course"]=>
        string(23) "Excel 2010 Intermediate"
        ["price"]=>
        string(3) "330"
        ["dates"]=>
        array(2) {
          [2]=>
          string(8) "Aug 27th"
          [0]=>
          string(8) "Jul 29th"
        }
      }
      [5]=>
      array(4) {
        ["city"]=>
        string(6) "Sydney"
        ["course"]=>
        string(19) "Excel 2010 Advanced"
        ["price"]=>
        string(3) "330"
        ["dates"]=>
        array(2) {
          [2]=>
          string(8) "Aug 28th"
          [0]=>
          string(8) "Jul 30th"
        }
      }
    }
    Code (markup):
    What I'm trying to get is:
    Brisbane
    Sydney

    My code:
                    <?php
                    $entries = get_option('form_options');
                   
                    $count = 0;
                   
                    foreach ( (array) $entries['demo'] as $key => $entry ) {
                       
                        $count++;
                   
                        $city = '';
                   
                        if ( isset( $entry['city'] ) )
                            $city = $entry['city'];
                    ?>
                   
                    <option class="<?php echo $city; ?>" value="<?php echo $city; ?>"><?php echo $city; ?></option>
                   
                    <?php } ?>
    Code (markup):
    With this code I get next:
    Brisbane
    Brisbane
    Brisbane
    Sydney
    Sydney
    Sydney
     
    secretdesign, Jul 21, 2015 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    You can hold the displayed city in a separate array.
    See code below:

    <?php
    $entries = get_option('form_options');
    $count = 0;
    
    $displayed_city = array();
    
    foreach((array) $entries['demo'] as $key => $entry){
      $count++;
      $city = '';
      if( isset($entry['city']))
        $city = $entry['city'];
        if(!in_array($city, $displayed_city)){
          $displayed_city[] = $city;
      ?>
      <option class="<?php echo $city; ?>" value="<?php echo $city; ?>"><?php echo $city; ?></option>
    <?php
       
        }
      }
    ?>
    PHP:
     
    krishmk, Jul 21, 2015 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Filter it as so:

    
    $entries = get_option('form_options');
    $unique_list = array_intersect_key(
        $entries['demo'],
        array_unique(array_map(function($item) {
            return $item['city'];
        }, $entries['demo']))
    );
    
    Code (markup):
    The resulting $unique_list would only contain the unique cities with no duplicates.
     
    ThePHPMaster, Jul 21, 2015 IP
    deathshadow and PoPSiCLe like this.
  4. secretdesign

    secretdesign Well-Known Member

    Messages:
    797
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    3
    #4
    Thank you guys.
     
    secretdesign, Jul 21, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    comparing how @krishmk and @ThePHPMaster approached it reminds me of something I'm always pointing out about PHP, particularly to people coming from other languages.

    "Yeah, there's a existing function for that."

    Quite often you see programmers resorting to the brute force approach when PHP is already able to do that for you.

    though guys, you do know that there is NO reason to EVER have a value identical to the content of an OPTION tag other than to intentionally cause code bloat, right? Much less it's pretty pointless to put classes on OPTION since technically OPTION are NOT supposed to receive style either? (even if FF does let you do so, little else does!)

    Also @ThePHPMaster, why the array intersect? Seems unnecessary.

    $entries = get_option('form_options');
    echo '
    	<label for="chooseCity">Choose City:</label>
    	<select name="city" id="chooseCity">
    		<option>', implode(
    			"</option>\n\t\t<option>",
    			array_unique(array_map(function($item) {
    				return $item['city'];
    			}, $entries['demo']))
    		), '</option>
    	</select>';
    Code (markup):
    Is how I'd be going about it.
     
    deathshadow, Jul 21, 2015 IP
    KangBroke and ThePHPMaster like this.
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    It is not necessary. I just left it there incase the OP wanted to keep the array structure the same, but if the names are the only thing needed your solution would be better.
     
    ThePHPMaster, Jul 21, 2015 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Thing is array_unique returns the indexes of the first encountered.... If you had:

    $test = [ 'alpha' => 1, 'bingo' => 2, 'charlie' => 1 ];

    an array_unique of that would return:

    ['alpha' => 1, 'bingo' => 2]

    As it DOES preserve indexes, it just removes elements whose values already exist.

    See the examples:
    http://php.net/manual/en/function.array-unique.php#refsect1-function.array-unique-examples

    Indexes are preserved for the first occurances, only the redundant values and their indexes are removed... which is actually pretty cool...

    More useful (not for this scenario, just talking in general) would be if one could put them into an array based on the value as a new key... something like:

    $filtered = [];
    foreach ($entries['demo'] as $key => $item) {
    	if (!isset($filtered[$item['city']])) {
    		$filtered[$item['city']] = [ $key ];
    	} else $filtered[$item['city']][] = $key;
    }
    Code (markup):
    Gives you unique city as the key, and the keys as a array indexing the original array if need be. I had thought PHP had a function to do that already, but it's my turn to forget what that function is. :p
     
    Last edited: Jul 21, 2015
    deathshadow, Jul 21, 2015 IP