开发者

Reading Parameters From An Array

I have an array of arrays:

Array ( [0] => Array ( [Name] => news/edit ) 
        [1] => Array ( [Name] => news/show ) ) 

I have the following two variables:

$module = 'news';
$action = 'show';

I want to se开发者_开发技巧e if my array contains news/show or $module/$action

I can use explode here, but I can only explode one array.


You can use array_search:

$array = array(array("Name" => "news/edit" ), array("Name" => "news/show"));
$module = 'news';
$action = 'show';
var_dump(array_search(array("Name" => "$module/$action"), $array));
// int(1)


Why don't you just concatenate the strings before checking? Like this:

function doesArrayContainModuleAction($array, $module, $action) {
    foreach($array as $subarray) {
        if($subarray['Name'] == "$module/$action") return true;
    }
    return false;
}


Are you searching for this?

$new_array=array_map(function($x){
     $y= explode('/',$x['Name']);
     return array('module'=>$y[0],'show'=>$y[1]);
},$array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜