1

I want to remove that 2 Justin Bieber elements from this array and display it in select tag.

This is the code I have:

<?php
$arr = array('Dream Theater', 'Animals as Leaders', 'Chimp Spanner', 'Periphery', 'Linkin Park', 'Metallica', 'Justin Bieber', 'Slipknot', 'Justin Bieber');
echo "<pre>";
print_r($arr);
?>
<select>
  <?php foreach ($arr as $value) {?>
      <option><?php echo $value?></option> //I do not want to echo "Justin Bieber" here
  <?php }?>
</select>
<?php
die;

4 Answers 4

3

Have you tried an if statement? Inside your foreach statement include :

if ($value!='justin bieber') {?>
//html code to be executed
<?php }?>
Sign up to request clarification or add additional context in comments.

2 Comments

Oops sorry for the errors. Doing this on my phone, I forget basic syntax
This is the fastest method as far as I can see. But it can become messy if you have lots of exclude values. It's only the fastest method if you only use the list once though. If you need the list without excludes again then all the work had to be done again.
1

You can do that with array-search (to find the string index) and unset.

Consider the following:

$arr = array('Dream Theater', 'Animals as Leaders', 'Chimp Spanner', 'Periphery', 'Linkin Park', 'Metallica', 'Justin Bieber', 'Slipknot', 'Justin Bieber');

$v = 'Justin Bieber';
while ($key = array_search($v, $arr))
    unset($arr[$key]);

Notice this will not fix your indexes (as you don't need to use them) but if you want them to be reset you can use $arr = array_values($arr);

1 Comment

Let OP choose the answer they want without pressure. Also note that the larger the array is the slower your method will be. Each time array_search is called it loops the array, and then you loop to use array_search. And it has to do one more search than matching items.
1

I believe you are looking for array_unique which will remove all duplicates from the array.

$arr = array('Dream Theater', 'Animals as Leaders', 'Chimp Spanner', 'Periphery', 'Linkin Park', 'Metallica', 'Justin Bieber', 'Slipknot', 'Justin Bieber');
$arr = array_unique($arr);

Now you only have one Justin Bieber.


To remove both Justin Bieber you can use array_diff.
This will return all values that is not "Justin Bieber" without looping.

$arr = array('Dream Theater', 'Animals as Leaders', 'Chimp Spanner', 'Periphery', 'Linkin Park', 'Metallica', 'Justin Bieber', 'Slipknot', 'Justin Bieber');
$arr = array_diff($arr, ["Justin Bieber"]);

https://3v4l.org/uLtqS

To reindex the keys (if needed) use array_values.

1 Comment

i was looking for excluding two value. anyways thanks for the reply
1
<?php
$bands    = ['Pink Floyd', 'The Animals', 'The Animals', 'TheThe', 'The Who', 'The Who'];
$counts   = array_count_values($bands);
$repeats  = array_filter($counts, function($v) { return $v>1;});
$filtered = array_diff($bands, array_keys($repeats));
?>
<select>
  <?php foreach ($filtered as $value) { ?>
      <option><?php echo $value?></option>
  <?php }?>
</select>

Output:

<select>
        <option>Pink Floyd</option>
        <option>TheThe</option>
  </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.