0

I have a array of val which has dynamic strings with underscores. Plus I have a variable $key which contains an integer. I need to match $key with each $val (values before underscore).

I did the following way:

<?php
    $key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array($key, $val)) {
    echo 'Yes'; 
} 
else
{
    echo 'No';  
}
?>

Though this code works fine, I want to know if its a correct way or suggest some better alternative.

3
  • Your code would output no - is it intended? If so, what actually do you expect simpler than a single function call? Commented Jan 7, 2013 at 7:40
  • But what's wrong with using this existing code? This is clean and recommended by PHP Manual itself, so why look for complicated versions. Theoretically there are "better" ways of doing everything, but that is subjective Commented Jan 7, 2013 at 7:58
  • since you need to match only the part before the underscore, you should use a regex match or a string match. the above code shouldn't work although you say that it works. hmmm... see answer below Commented Jan 7, 2013 at 8:15

5 Answers 5

1

use this function for regex match from php.net

function in_array_match($regex, $array) {
    if (!is_array($array))
        trigger_error('Argument 2 must be array');
    foreach ($array as $v) {
        $match = preg_match($regex, $v);
        if ($match === 1) {
            return true;
        }
    }
   return false;
}

and then change your code to use this function like this:

$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array_match($key."_*", $val)) {
    echo 'Yes'; 
} 
else
{
    echo 'No';  
}
Sign up to request clarification or add additional context in comments.

Comments

1

This should work :

foreach( $val as $v ) 
{
    if( strpos( $v , $key .'_' ) === true ) 
    {
        echo 'yes';
    }
    else {
        echo 'no';
    }
}

1 Comment

although i +1d this, you should note that since strpos will return 0 if the match is found, you should do the comparison using ===. so it should be changed to if( strpos( $v .'_' , $key ) === true)
0

you can use this

    function arraySearch($find_me,$array){
$array2 =array();
foreach ($array as $value) {
    $val = explode('_',$value);
    $array2[] =$val[0]; 
}
$Key = array_search($find_me, $array2);
$Zero = in_array($find_me, $array2);
if($Key == NULL && !$Zero){
    return false;
}
return $Key;
}

Comments

0
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
$inarray = false;
foreach($val as $v){
    $arr = explode("_", $val);
    $inarray = $inarray || $arr[0] == $key
}
echo $inarray?"Yes":"No";

9 Comments

$val is an array, not a string
@zerkms Right, missed that. Thanks.
1. It's php5.4 only 2. It's a huge overcomplication. strpos($val, $key) === 0 - would do the job better 3. you probably want to break; after you find entry.
Seeing your answer, I'm confused with the answer that I gave. I thought the OP wanted to check the value before the underscore, did I misunderstand the question ?
@zerkms Point 2 is wrong, by the way. strpos is fooled by numbers that begin with the same digits but aren't exactly the same. Searching for 2 in "25_55" for example.
|
0

The given format is quite unpractically.

$array2 = array_reduce ($array, function (array $result, $item) {
     list($key, $value) = explode('_', $item);
     $result[$key] = $value;
     return $result;
}, array());

Now you can the existence of your key just with isset($array2[$myKey]);. I assume you will find this format later in your execution useful too.

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.