Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP使用索引部分字符串查找数组值_Php_Arrays_String - Fatal编程技术网

使用PHP使用索引部分字符串查找数组值

使用PHP使用索引部分字符串查找数组值,php,arrays,string,Php,Arrays,String,我有下面的数组。我想获取具有“1”的值,并且键应该是“wf\u status\u step%”。如何为此编写PHP脚本 [ini_desc] => 31.07 Initiative1 [mea_id] => 1 [status] => 4 [name] => 31.07 Measure1 [scope] => NPR [sector] => [mea_commodity] => 8463 [commodity_cls] => IT [delega

我有下面的数组。我想获取具有“1”的值,并且键应该是“wf\u status\u step%”。如何为此编写PHP脚本

[ini_desc] => 31.07 Initiative1
[mea_id] => 1
[status] => 4
[name] => 31.07 Measure1
[scope] => NPR
[sector] => 
[mea_commodity] => 8463
[commodity_cls] => IT
[delegate_usrid] => 877
[wf_status_step1] => 2
[wf_status_step2] => 1
[wf_status_step3] => 0
[wf_status_step4] => 0
[wf_status_step5] => 0
长话短说

foreach($your_array as $key=>$value)
{
  if(strpos($key, 'f_status_step') !== FALSE) // will check for existence of "f_status_step" in the keys
  {
     if($value == 1) // if the value of that key is 1
     {
       // this is your target item in the array
     }
  }
}
长话短说

foreach($your_array as $key=>$value)
{
  if(strpos($key, 'f_status_step') !== FALSE) // will check for existence of "f_status_step" in the keys
  {
     if($value == 1) // if the value of that key is 1
     {
       // this is your target item in the array
     }
  }
}

您可以迭代数组中的键,以找到与您的模式匹配的所有键,并模拟地检查关联的值。大概是这样的:

<?php
$found_key = null;
foreach(array_keys($my_array) as $key) {
    if(strpos($key, "wf_status_step") === 0) {
        //Key matches, test value.
        if($my_array[$key] == 1) {
            $found_key = $key;
            break;
        }
    }
}
if( !is_null($found_key) ) {
    //$found_key is the one you're looking for
} else {
    //Not found.
}
?>

您可以迭代数组中的键,以找到与您的模式匹配的所有键,并模拟检查关联的值。大概是这样的:

<?php
$found_key = null;
foreach(array_keys($my_array) as $key) {
    if(strpos($key, "wf_status_step") === 0) {
        //Key matches, test value.
        if($my_array[$key] == 1) {
            $found_key = $key;
            break;
        }
    }
}
if( !is_null($found_key) ) {
    //$found_key is the one you're looking for
} else {
    //Not found.
}
?>

一个较短的版本,将查找以“wf\u status\u step”开头的值为1的所有键

$keys = array_filter(array_keys($array,1),function($key){
    return stripos($key,'wf_status_step') === 0;
});

较短的版本,将查找以“wf\U状态\U步骤”开头的值为1的所有键

$keys = array_filter(array_keys($array,1),function($key){
    return stripos($key,'wf_status_step') === 0;
});
试试这个

   $wf_status_array = array();
    foreach ($array as $key => $value) {
        if($value === 1 && preg_match_all('~^wf_status_step[0-9]+$~',$key,$res)){
            $key = $res[0][0];
            $wf_status_array[$key] = $array[$key];
        }
    }
    print_r($wf_status_array)
试试这个

   $wf_status_array = array();
    foreach ($array as $key => $value) {
        if($value === 1 && preg_match_all('~^wf_status_step[0-9]+$~',$key,$res)){
            $key = $res[0][0];
            $wf_status_array[$key] = $array[$key];
        }
    }
    print_r($wf_status_array)

但这也会匹配像“这是错误的、关键的、状态的”这样的键。检查
strpos
是否实际返回0不是更好吗?是的,可以,但是根据OP数组中的键,这将正常工作。当然,OP没有提到按键可能不同,但也会匹配“这是错误的按键状态步骤”这样的按键。检查
strpos
是否实际返回0不是更好吗?是的,可以,但是根据OP数组中的键,这将正常工作。当然,OP没有提到按键可能不同,但也会匹配“这是错误的按键状态步骤”这样的按键。检查
strpos
是否实际返回0不是更好吗?但这也会匹配“this\u is\u the \u the \u the错误的\u key\u wf\u status\u step”这样的键。检查
strpos
是否实际返回0不是更好吗?有趣的方法,但这只是问题的一半。OP仍然需要在founds键上迭代,看看哪个键具有所需的值。@sh1ftst0rm第二个参数可以解决这个问题哦,太好了。我甚至没有意识到这一点+1.有趣的方法,但这只是问题的一半。OP仍然需要在founds键上迭代,看看哪个键具有所需的值。@sh1ftst0rm第二个参数可以解决这个问题哦,太好了。我甚至没有意识到这一点+1.