search

Home  >  Q&A  >  body text

Make PHP ignore strings

Array 1:

    $Arr1 = array(
            'name/?' => 'apple',
            'weight' => '30kg',
    );

Now we need to check whether isset( $Arr1['name/fruits'] ) exists in array 1. Key points:
How to let PHP put it? Ignore fruits and return true

扔个三星炸死你扔个三星炸死你2847 days ago629

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-06-07 09:24:55

    function my_isset($array, $key){
        foreach($array as $k => $v) {
            if (strpos($k, '?') == -1) {
                if ($k == $key) {
                    return true;
                }
            } else {
                $pattern = str_replace('?', '.+', $k);
                $pattern = str_replace('/', '\/', $pattern);
                if (preg_match('/'. $pattern . '/', $key)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    $a = [
        'aa' => 'dddd',
        'xx/?' => 'dd',
    ];
    
    var_dump(my_isset($a, 'aa'), my_isset($a, 'xx/dddd'), my_isset($a, 'xxx'));

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-07 09:24:55

    According to your business logic, you only need to determine whether key is in name, that is, whether name/ can be found in name/fruits:
    strpos($name , substr($ key , 0 , -1))

    reply
    0
  • Cancelreply