0

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.

3 Answers 3

2
$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?)

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

1 Comment

Wow! This is such a simple and elegant solution! Thank you very much deifwud. Thank you.
1

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.

1 Comment

Thank you very much. It is impressive but I believe the answer above is a bit more simpler.
1

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;
}

?>

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.