Array value find using index partial string using PHP -
i having below array. value having '1' , key should 'wf_status_step%'. how write php script this?
[ini_desc] => 31.07 initiative1 [mea_id] => 1 [status] => 4 [name] => 31.07 measure1 [scope] => npr [sector] => [mea_commodity] => 8463 [commodity_cls] => [delegate_usrid] => 877 [wf_status_step1] => 2 [wf_status_step2] => 1 [wf_status_step3] => 0 [wf_status_step4] => 0 [wf_status_step5] => 0
you can iterate on keys in array find keys match pattern, , simulatenously check associated values. this:
<?php $found_key = null; foreach(array_keys($my_array) $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 1 you're looking } else { //not found. } ?>
if want more sophisticated matching key, can use regular expressions.
you can use foreach($my_array $key=>$value)
mechanism shown in other answers, instead of using array_keys
.
Comments
Post a Comment