0

I have the following PHP array:

Array
(
    [0] => 750
    [1] => 563
    [2] => 605
    [3] => 598
    [4] => 593
)

I need to perform the following action on the array using PHP:

  1. Search the array for a value (the value will be in a variable; let's call it $number). If the value is present in the array, remove it.

If someone could walk me through how to do that, it would be much appreciated.

Note: If it makes it any easier, I can form the array so the keys are the same as the values.

3
  • 1
    For the god sake - read the documentation ru2.php.net/manual/en/ref.array.php Commented Sep 19, 2011 at 4:51
  • These are 3 completely separate questions. Look at Finding Min and Max questions for number 3. Commented Sep 19, 2011 at 4:54
  • OK, I've managed to find the min and max, and remove duplicates, but how do I remove a specific value if it exists? I'll edit the question to house only this. Commented Sep 19, 2011 at 4:56

3 Answers 3

2
$array = array_unique($array) // removes dupicate values


while(false !== ($num = array_search($num, $array))){
    unset($array[$num]);
}
$max = max($array);

will search for all keys with value $num and unset them

Sign up to request clarification or add additional context in comments.

7 Comments

The while loop should check specifically for !== false because if the first element (index 0) is found, your code will not unset the element.
@genesis φ - Thanks for the answer. How do you feel while compares with Josh Randall's method of unsetting the key?
@stefmikhail: his one is little bit complicated and harder.
@genesis φ - Don't know if you saw my original question, but it was to explain how to remove duplicates as well as place the maximum value in a variable. How do you feel about Josh Randall's answer concerning those?
@stefmikhail: no, second step is wrong. I'll add it into my answer
|
2

lets say your $array

$array = array_unique($array) // removes dupicate values

$array = arsort($array) 

$variable = $array[0]  //  the maximum value in the array, and place it in a variable.  

$key = array_search($array, $number);

if($key){

    unset($array[$key])  // Search array for a value, value is present in array, remove it.

}

Comments

1

array_search() and unset() seems a good method for your sample data in your question. I'll just show a different way for comparison's sake (or in case your use case is slightly different from what you have posted here).

Methods: (Demo)

$array=[750,563,605,598,593];

// if removing just one number apply the number as an array element
$number=605;
var_export(array_diff($array,[$number]));

// if you are performing this task with more than one $number, make $numbers=array() and do the same...
$numbers=[605,563];  // order doesn't matter
var_export(array_diff($array,$numbers));

// if you need to re-index the output array, use array_values()...
$numbers=[605,563];  // order doesn't matter
var_export(array_values(array_diff($array,$numbers)));

Output:

array (
  0 => 750,
  1 => 563,
  3 => 598,
  4 => 593,
)

array (
  0 => 750,
  3 => 598,
  4 => 593,
)

array (
  0 => 750,
  1 => 598,
  2 => 593,
)

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.