基于另一个数组过滤 $_REQUEST 数组?

发布于 2024-12-17 15:52:36 字数 437 浏览 0 评论 0原文

数组 $ALLOWED_CALLS 包含函数名称和所需参数。我想过滤 $_REQUEST 数组以获得仅包含必需参数的 $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']

Array $ALLOWED_CALLS contains a function name and required parameters. I'd like to filter $_REQUEST array obtaining a $params array with only required paramets in it. How to?

$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']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2024-12-24 15:52:36

我会像这样使用 array_intersect_key()

$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]));

I'd use array_intersect_key() like so:

$params = array_intersect_key($_REQUEST, array_flip($ALLOWED_CALLS[$call]));

Thus, the whole thing:

$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]));
梦归所梦 2024-12-24 15:52:36

像这样的东西:

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

Something like:

$contains_required = true;
foreach( $ALLOWED_CALLS[$call] as $key => $value )
{
    if(!in_array($value, $_REQUEST))
    {
        $contains_required = false;
    }
}
奈何桥上唱咆哮 2024-12-24 15:52:36
function getParams ($call, $allowedCalls) {
  // Return FALSE if the call was invalid
  if (!isset($allowedCalls[$call])) return FALSE;
  // Get allowed params from $_REQUEST into result array
  $result = array();
  foreach ($allowedCalls[$call] as $param) {
    if (isset($_REQUEST[$param])) {
      $result[$param] = $_REQUEST[$param];
    }
  }
  // Return the result
  return $result;
}

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

function getParams ($call, $allowedCalls) {
  // Return FALSE if the call was invalid
  if (!isset($allowedCalls[$call])) return FALSE;
  // Get allowed params from $_REQUEST into result array
  $result = array();
  foreach ($allowedCalls[$call] as $param) {
    if (isset($_REQUEST[$param])) {
      $result[$param] = $_REQUEST[$param];
    }
  }
  // Return the result
  return $result;
}

Returns the $params array, or FALSE on failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文