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

4 Answers

up vote 8 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

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
show 2 more comments

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

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

Your Answer

 
or
required, but never shown
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.