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.

Removing duplicates from php array?

Discussion in 'PHP' started by tdd1984, Apr 25, 2018.

  1. #1
    Hi how do I remove the duplicate desiredjobposition from this array below?

    <?PHP
    $array = json_decode($json_string);
    
    //exclude duplicates, only unique categories should come back
    
    
    //start of our select menu
    echo "<select>";
    //Our Categories Menu
    foreach($array->results as $key => $object){
    
      ?>
    
      <option value="<?PHP echo $object->meta_data-> desiredjobposition[0];?>"><?PHP echo $object->meta_data-> desiredjobposition[0];?></option>
    
      
    <?PHP }
    echo "</select>";
    ?>
    PHP:

     
    tdd1984, Apr 25, 2018 IP
  2. WebDirectoryGuy

    WebDirectoryGuy Greenhorn

    Messages:
    7
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    13
    #2
    $unique = array_unique($array);
    PHP:
     
    WebDirectoryGuy, Apr 25, 2018 IP
    deathshadow likes this.
  3. AlphaNine_Vini

    AlphaNine_Vini Active Member

    Messages:
    218
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    88
    #3
    Yes, array_unique will remove the duplicate array from the array. It functions on PHP : 4.0.1+
     
    AlphaNine_Vini, Apr 25, 2018 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    As mentioned that function will do what you ask, but it also might help if you weren't opening and closing PHP willy-nilly and using the most inefficient method of echo possible. Also if the value is the same as the HTML content of an OPTION tag, you don't need to say value="". You only say value="" when the value is DIFFERENT from the content text. (or in some strange javascript-tard corner cases involving IE8/earlier)

    
    <?php
    
    $array = json_decode($json_string);
    $results = array_unique($array->results);
    
    echo '
    	<select>';
    	
    foreach ($results as $object) echo '
    		<option>', $object->meta_data->desiredjobposition[0], '</option>';
    
    echo '
    	</select>';
    
    Code (markup):
    functionally identical, with the array_unique reduction implemented. Single quotes and comma delimits FTMFW.

    Oh, I also killed off $key since you weren't using it.
     
    deathshadow, Apr 29, 2018 IP