Jump to content

[SOLVED] Remove element from array


PHPTOM

Recommended Posts

If you want to search for an element, and then remove it, you can use array_search().

 

<?php
$ar = array("test", "php", "freak");
$rem = 'test';
unset($ar[array_search($rem)]);
//$ar == array("php", "freak");
?>

 

And it will only work for the first occurrence of the searched string. If you want to remove all occurrences of an element, read up on using array_keys() with the optional search_value parameter.

THanks for all the responses. Basically I had a range from 1-80, then I had to do a while from the database to remove certain numbers. So then I could select  a random number what hasn't already been chosen. Unset works fine, because  I shuffle it afterwords. Thanks again! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.