2

How can i check if a string is already present withing an array before adding to it in php?

say if array is $value

$value[0]="hi";
$value[1]="hello"; 
$value[2]="wat";

I want to check if value exists in it before adding it to it.

3
  • do you imply if "hi" was present before, it will not be inserted again? Commented Jul 7, 2011 at 17:10
  • 4
    Using your exact words: "How can i check if a string is already present withing an array before adding to it in php?" comes up with the manual for in_array() as the first hit in google. -1 For the "no research effort" clause Commented Jul 7, 2011 at 17:13
  • @stereorog: Then that needs to be explicitly specified, at least that OP knows that the function exists but is not what he wants - look at the current answers. Commented Jul 7, 2011 at 17:27

5 Answers 5

5
$s='hi';
if (!in_array($s, $value)) {
    $value[]=$s;
}
  • in_array() checks if that value (1st parameter) is in the array (2nd parameter), returns boolean (! negates it)

  • $value[]=$s will add the value to the array with the next index


There is another tricky way if you want to add a bunch of values into an array, but only if they are not there yet. You just have to organize these new values into another array and use a combination of array_merge() and array_diff():

//your original array:
$values=array('hello', 'xy', 'fos', 'hi');
//the values you want to add if they are not in the array yet:
$values_to_add=array('hi', 'hello', 'retek');

$values=array_merge($values, array_diff($values_to_add, $values));
//$values becomes: hello, xy, fos, hi, retek
Sign up to request clarification or add additional context in comments.

2 Comments

Needs a ! before in_array
should be if(!in_array(...))
2

You can use in_array($searchstring, $array)

in_array("hello", $value) returns true

in_array("hllo", $value) returns false

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

Comments

1

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

if(in_array("hello", $value)) { // needle in haystack
   return TRUE;
}

Comments

1

in_array - Checks if a value exists in an array.

In case of in_array($searchstring, $array), remember, the comparison is done in a case-sensitive manner, if the $searchstring is a "String"

Example :

 - in_array("wat", $value) returns true
 - in_array("what", $value) returns false.

 // Observe carefully
 - in_array("WAT", $value) returns false.

Comments

1

if you're going to create a Set, it's much better to use array keys instead of values. Simply use

$values["whatever"] = 1;

to add a value, no need to check anything.

1 Comment

This is not quite the same in the current context, it means you must use keys instead of values. OP said he wants to check values. This also doesn't "check" anything, so is not useful for any situation where we would need to do so, and especially in situations where the keys are not explicitly indexed, i.e. array('apple', 'orange', 'banana').

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.