Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to find a value exist in an array and how to remove it. If any php builtin array functions for doing this. After removing I need the sequential index order. any body knows please help me.

share|improve this question

7 Answers 7

up vote 30 down vote accepted

To search an element in an array, you can use array_search function and to remove an element from an array you can use unset function. Ex:

<?php
$hackers = array ('Alan Kay', 'Peter Norvig', 'Linus Trovalds', 'Larry Page');

print_r($hackers);

// Search
$pos = array_search('Linus Trovalds', $hackers);

echo 'Linus Trovalds found at: ' . $pos;

// Remove from array
unset($hackers[$pos]);

print_r($hackers);
?>

You can refer: http://www.php.net/manual/en/ref.array.php for more array related functions.

share|improve this answer
3  
But if array_search returns false because of nothing found, the first array value will be removed. –  algorhythm Sep 8 '14 at 15:20
    
What if there is no exact match? how to perform a wildcard search? –  Clain Dsilva Nov 12 '14 at 2:26
<?php
$my_array = array('sheldon', 'leonard', 'howard', 'penny');
$to_remove = array('howard');
$result = array_diff($my_array, $to_remove);
?>
share|improve this answer
1  
that is very nice –  Alex Feb 7 '14 at 10:06
4  
Can remove multiple values without looping! –  dfmiller Mar 4 '14 at 22:15
    
+1 avoids the tedium of checking the return value of array_search –  rinogo Jul 15 '14 at 14:25
    
+1 Nice solution –  algorhythm Sep 8 '14 at 16:49
    
Seems to be more efficient, the array $to_remove can be created using an array_search or preg_grep function in case if you are using wild card search to remove elements. –  Clain Dsilva Nov 12 '14 at 2:30

You need to find the key of the array first, this can be done using array_search()

Once done, use the unset()

<?php
$array = array( 'apple', 'orange', 'pear' );

unset( $array[array_search( 'orange', $array )] );
?>
share|improve this answer
    
This is the result Array ( [0] => apple [1] => orange [2] => pear [3] => green ) Warning: Wrong parameter count for array_search() in C:\wamp\www\test\test.php on line 5 Array ( [0] => apple [1] => orange [2] => pear [3] => green ) –  learner Jun 17 '10 at 6:43
1  
@learner the haystack argument was missing in de3.php.net/manual/en/function.array-search.php - the manual is your friend. –  Gordon Jun 17 '10 at 6:45
    
yes. this will work $array = array( 'apple', 'orange', 'pear', 'green' ); unset($array[array_search('orange', $array)]); but the array sequence is missing. How to correct that –  learner Jun 17 '10 at 6:49
    
What do you mean the sequence is missing? What sequence should it be in? –  Kerry Jun 17 '10 at 6:53
    
array index is 0 2 3 4 is now 1 is missing I need it like 0 1 2 4.. etc –  learner Jun 17 '10 at 6:54

Just in case you want to use any of mentioned codes, be aware that array_search returns FALSE when the "needle" is not found in "haystack" and therefore these samples would unset the first (zero-indexed) item. Use this instead:

<?php
$haystack = Array('one', 'two', 'three');
if (($key = array_search('four', $haystack)) !== FALSE) {
  unset($haystack[$key]);
}
var_dump($haystack);

The above example will output:

Array
(
    [0] => one
    [1] => two
    [2] => three
)

And that's good!

share|improve this answer
    
+1 It's work perfectly –  algorhythm Sep 8 '14 at 16:48

This solution is the combination of @Peter's solution for deleting multiple occurences and @chyno solution for removing first occurence. That's is what I#m using.

/**
 * @param array $haystack
 * @param mixed $value
 * @param bool $only_first
 * @return array
 */
function array_remove_values(array $haystack, $needle = null, $only_first = false)
{
    if (!is_bool($only_first)) { throw new Exception("The parameter 'only_first' must have type boolean."); }
    if (empty($haystack)) { return $haystack; }

    if ($only_first) { // remove the first found value
        if (($pos = array_search($needle, $haystack)) !== false) {
            unset($haystack[$pos]);
        }
    } else { // remove all occurences of 'needle'
        $haystack = array_diff($haystack, array($needle));
    }

    return $haystack;
}

Also have a look here: PHP array delete by value (not key)

share|improve this answer
    
+1 from my side, for your SUPERB answer, and pointing out my fault! Oh, BTW, you can also edit anyone's answer and update it, along with putting your name, to mark the wrong part of it. –  Knowledge Craving Sep 8 '14 at 17:51

First of all, as others mentioned, you will be using the "array_search()" & the "unset()" methodsas shown below:-

<?php
$arrayDummy = array( 'aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg' );
unset( $arrayDummy[array_search( 'dddd', $arrayDummy )] ); // Index 3 is getting unset here.
print_r( $arrayDummy ); // This will show the indexes as 0, 1, 2, 4, 5, 6.
?>

Now to re-index the same array, without sorting any of the array values, you will need to use the "array_values()" method as shown below:-

<?php
$arrayDummy = array_values( $arrayDummy );
print_r( $arrayDummy ); // Now, you will see the indexes as 0, 1, 2, 3, 4, 5.
?>

Hope it helps.

share|improve this answer
1  
But if array_search returns false because of nothing found, the first array value will be removed. –  algorhythm Sep 8 '14 at 15:23
    
@algorhythm - Thanks for pointing that out! I will suggest that everyone should use the solution provided by you! –  Knowledge Craving Sep 8 '14 at 17:48

To find and remove multiple instance of value in an array, i have used the below code

$list = array(1,3,4,1,3,1,5,8);

$new_arr=array();

foreach($list as $value){

    if($value=='1')
    {
        continue;
    }
    else
    {
        $new_arr[]=$value;
    }     
}


print_r($new_arr);
share|improve this answer
    
It's not using PHP array functions as requested... –  algorhythm Sep 8 '14 at 15:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.