Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am fairly new to php so I am not sure of the names and terms of what I would like to find out here. I did do a search on SE but although the titles of questions are similar, they ask a completely different thing which are unfortunately not partially related even. Excuse me in advance if it exists and I could not find.

I have a string $str= 'somevalue'; or an $array = ['s', 'o', 'm' ..];

Now, I have an other array which is 2 dimensional, of which 1st items I want to check against this main array and depending on whether they exist or not, add the 2nd item.

$to_match[] = ('match' => 'rnd_letter', 'if_not_add' => 'someval');
$to_match[] = ('match' => 'rnd_letter', 'if_not_add' => 'someval_x');
..

rnd_letter is a letter or combination of letters and someval is the same.

How can I check if letter(s) in 'match' exists in $str, and if not, add to the end letters of 'if_not_add' of the array?

Thank you very much.

share|improve this question

3 Answers

up vote 2 down vote accepted
$to_match = array();
$to_match[] = array('match' => 'hello', 'if_not_add' => 'value 1');
$to_match[] = array('match' => 'abc', 'if_not_add' => 'value 2');
$to_match[] = array('match' => 'w', 'if_not_add' => 'value 3');

$str = 'Hello World!';
$new_array = array();

foreach($to_match as $value) {
  if(!stristr($str, $value['match'])) {
    $new_array[] = $value['if_not_add'];
  }
}

var_dump($new_array); // outputs array(1) { [0]=> string(7) "value 2" } 

This will iterate over each array element, then check if the value of match exists in $str, if not it will add it to $new_array (I think that's what you were looking for?)

share|improve this answer
Wow! This is such a simple and elegant solution! Thank you very much deifwud. Thank you. – Phil May 14 '12 at 10:27

String:

for (int i = 0; i < strlen($to_match['match']); i++) {
    $char = substr($to_match['match'], i, 1);
    if (strpos($str, $char) !== false) {
        //contains the character
    } else {
        //does not contain the character
    }
}

Array:

for(int i = 0; i < strlen($to_match['match']); i++) {
    $char = substr($to_match['match'], i, 1);
    $charFound = false;
    for (int j = 0; j < count($array); j++) {
        if ($char == $array[j]) {
            $charFound = true;
        }
    }

    if ($charFound) {
        //it contains the char
    } else {
        //it doesnt contain the char
    }
}

It should be something like this i suppose. Let me know what you think of this.

share|improve this answer
Thank you very much. It is impressive but I believe the answer above is a bit more simpler. – Phil May 14 '12 at 10:37

you can check whether a string is existed in an array using following way

<?php 
$array = array('mike','sam','david','somevalue');
$str = 'somevalue';
if(in_array($str,$array)){
    //do whatever you want to do
    echo $str;
}

?>
share|improve this answer

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.