Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Request - Fatal编程技术网

Php 是否基于另一个数组筛选$\请求数组?

Php 是否基于另一个数组筛选$\请求数组?,php,arrays,request,Php,Arrays,Request,数组$ALLOWED\u CALLS包含函数名和必需的参数。我想过滤$\u请求数组,获得一个$params数组,其中只包含所需的参数。怎么做 $call = 'translate'; $ALLOWED_CALLS = array( 'getLanguages' => array(), 'detect' => array('text'), 'translateFrom' => array('text', 'from', 'to'), 'trans

数组
$ALLOWED\u CALLS
包含函数名和必需的参数。我想过滤
$\u请求
数组,获得一个
$params
数组,其中只包含所需的参数。怎么做

$call = 'translate';

$ALLOWED_CALLS = array(
    'getLanguages' => array(),
    'detect' => array('text'),
    'translateFrom' => array('text', 'from', 'to'),
    'translate' => array('text', 'to'),
);

$params = array(); // Should contain $_REQUEST['text'] and $_REQUEST['to']
看一看

看一看类似的东西:

$contains_required = true;
foreach( $ALLOWED_CALLS[$call] as $key => $value )
{
    if(!in_array($value, $_REQUEST))
    {
        $contains_required = false;
    }
}
比如:

$contains_required = true;
foreach( $ALLOWED_CALLS[$call] as $key => $value )
{
    if(!in_array($value, $_REQUEST))
    {
        $contains_required = false;
    }
}
我想这样用:

因此,整个事情:

$call = 'translate';

$ALLOWED_CALLS = array(
    'getLanguages' => array(),
    'detect' => array('text'),
    'translateFrom' => array('text', 'from', 'to'),
    'translate' => array('text', 'to'),
);

$params = array_intersect_key($_REQUEST, array_flip($ALLOWED_CALLS[$call]));
我想这样用:

因此,整个事情:

$call = 'translate';

$ALLOWED_CALLS = array(
    'getLanguages' => array(),
    'detect' => array('text'),
    'translateFrom' => array('text', 'from', 'to'),
    'translate' => array('text', 'to'),
);

$params = array_intersect_key($_REQUEST, array_flip($ALLOWED_CALLS[$call]));
返回$params数组,或在失败时返回
FALSE


返回$params数组,或在失败时返回
FALSE

更好的是,使用array\u intersect\u keyYes,效果会更好。我只是希望给OP指出正确的方向,这样他就可以自己解决;-)更好的是,数组_intersect _key是的,这样会更好。我只是希望给OP指出正确的方向,这样他就可以自己解决;-)+我还可能检查$call是否作为$ALLOWED\u CALLS中的一个键存在,以确保完整性。@LikurVicar当然可以,对此不能争辩。另一件可能需要的事情是检查
文本
(在本例中)是否都存在于预期的位置。按原样,如果它们存在于
$\u REQUEST
中,则此代码将它们包含在
$params
中,但目前无法确保它们确实存在于第一位。+1可能还会检查$call是否作为$ALLOWED\u CALLS中的键存在,以确保完整性。@LikurVicar当然,对此无法反驳。另一件可能需要的事情是检查
文本
(在本例中)是否都存在于预期的位置。按原样,如果它们存在于
$\u REQUEST
中,则此代码将它们包含在
$params
中,但目前无法确保它们首先存在。