4

Say I have an array:

$before = array(1,2,3,3,4,4,4,5)

How would I remove just one occurence of eg '3' ?

$after = array(1,2,3,4,4,4,5)

(I don't know where in the array that number will be)

1
  • 1
    if you need an answer specific for this case, it'll be different than general case. Commented Mar 23, 2012 at 19:29

5 Answers 5

5

You can use a variety of methods, depending on what exactly you're trying to do:

  • Find the index using array_search() and then unset() it.
  • If you know the index, use array_splice() to replace it with nothing.
  • Use array_filter() with a custom callback.
Sign up to request clarification or add additional context in comments.

1 Comment

For example: unset($before[array_search(3,$before)]);. Removes the first occurrence of 3 in the array.
1

Generic routine, just populate the $reduceValues array with the values you want reducing to singletons.

$before = array(1,2,2,2,3,3,4,4,4,5,5);

$reduceValues = array(3,5);
$toReduce = array_fill_keys($reduceValues,TRUE);
$after = array_filter( $before,
                       function($data) use ($reduceValues,&$toReduce) {
                           if (in_array($data,$reduceValues)) {
                               if ($toReduce[$data]) {
                                   $toReduce[$data] = FALSE;
                                   return TRUE;
                               }
                               return FALSE;
                           }
                           return TRUE;
                       }
                     );
var_dump($after);

1 Comment

@salathe - and there was me just working on a more generic version that would allow multiple "targets" to be reduced to a single entry.
0

Is this the functionality you are looking for?

http://php.net/manual/en/function.array-filter.php

Comments

0

If you know the order of specific item you want to remove you can use something like

 unset $before[2]

Comments

0

There are several ways to do what you ask. Which one you should use depends on the context of its usage, as well as the exact result you desire. I prefer array_splice(), because it maintains the array numbering. If you want the easiest method, I suggest unset(). If you are looking for a way to eliminate the dupes, use array_unique(). With the latter two, if you need to renumber them back the way they were, use array_values(). If you do not know the index of the value you need to remove, you can use one of the above in conjunction with array_search(). If you need a more advanced filter, use array_filter().

<?php
header('Content-Type: text/plain');

$original = array(1,2,3,3,4,4,4,5);

echo "ORIGINAL:\n";
print_r($original);
echo "\n\n";

echo "Using array_splice():\n";
$new = $original;
array_splice($new, 3, 1);
print_r($new);
echo "\n\n";

echo "Using array_unique() to remove dupes:\n";
$new = array_unique($original);
$new = array_values($new);
print_r($new);
echo "\n\n";

echo "Using unset:\n";
$new = $original;
unset($new[3]);
$new = array_values($new);
print_r($new);
echo "\n\n";

echo "Using array_search & unset:\n";
$new = $original;
unset($new[array_search('3', $new)]);
$new = array_values($new);
print_r($new);
echo "\n\n";
?>

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.