0

I am having below array. I would like to get the value which is having '1' and the key should be 'wf_status_step%'. How to write a PHP script for this?

[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

5 Answers 5

6

A shorter version that will find all keys with value 1 that start with 'wf_status_step'

$keys = array_filter(array_keys($array,1),function($key){
    return stripos($key,'wf_status_step') === 0;
});
3
  • Interesting approach, but that's only half the problem. The OP still needs to iterate over the founds keys and see which one has the desired value. Commented Aug 7, 2013 at 18:07
  • 2
    @sh1ftst0rm the second argument to array_keys takes care of that
    – omma2289
    Commented Aug 7, 2013 at 18:07
  • Oh, nice. I didn't even realize that. +1. Commented Aug 7, 2013 at 18:08
0

Long answer

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
     }
  }
}
2
  • But that will also match keys like "this_is_the_wrong_key_wf_status_step". Isn't it better to check that strpos actually returns 0? Commented Aug 7, 2013 at 17:51
  • Yes that would, but going by the keys present in the OP's array, this will work fine. Of course, the OP didn't mention that the keys might be differ too
    – asprin
    Commented Aug 7, 2013 at 17:52
0

You can iterate over the keys in the array to find all the keys that match your pattern, and simulatenously check associated values. Something like this:

<?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.
}
?>

If you want to be more sophisticated about matching the key, you can use regular expressions.

You can also use the foreach($my_array as $key=>$value) mechanism that is shown in some other answers, instead of using array_keys.

0

try this

   $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)
0
foreach ($array_name as $key => $value) {
  if (strpos($key, 'wf_status_step') === 0) {
    if ($value == 1) {
      // do something
    }
  }
}
1
  • But that will also match keys like "this_is_the_wrong_key_wf_status_step". Isn't it better to check that strpos actually returns 0? Commented Aug 7, 2013 at 17:51

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.