问题描述
比方说我的 $ _ POST
变量如下:
< PHP排列
(
[USER_ID] => 65
[动作] => editpost
[originalaction] => editpost
[post_author] => 154
[empl_bd_dd] => 6
[empl_bd_mm] =>五
[empl_bd_yy] => 1987年
[empl_gen] => 1
[empl_height] => 155
[empl_weight] => 61
[empl_arra] => 2
[保存] =>更新
[post_it] => 2
[empl_pay] => J77
[empl_cust] =>已婚
[empl_lang] =>排列
(
[0] => EN
[1] => FR
)
[empl_rent] => 1
[名] =>吉米·内森
[empl_text] => Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。 Donec sed的interdum狮子座。桑达等ELIT华富,tempor placerat预期值,也。 Nullam SAPIEN奥迪奥,egestas iaculis格言UT,congue ullamcorper得力士。
[empl_sk_0] => 6
[empl_sk_1] => 7
[empl_sk_2] =>五
)?>
正如你可以看到我prefixed我所有的表单变量与 EMPL _
。短指定所有逐一的,我如何才能从 $ _ POST
我所有的表单变量到一个数组中的最低成本有望优雅的方式呢?是否有一个PHP阵列功能或它们的组合,我可以用这个?
像 CSS
在这里你可以选择具有与开始EMPL
使用<$ C类的所有元素C> [*类=_ EMPL] ,是有办法,我可以在PHP中,如:
$ empl_post = $ _ POST ['empl_ *']
EDITED答案 - IMPT修正@克里斯的回答是: $ _ POST
有可能成为第一个参数 array_intersect_key
,例如:
$ empl_POST = array_intersect_key($ _ POST,array_flip(preg_grep('/ ^ empl_ /',array_keys($ _ POST))));
$ R = array_intersect_key($ _ POST,array_flip(preg_grep('/ ^ empl_ /',array_keys( $ _ POST))));
他们真正需要添加一个 preG_GREP_KEYS
标记,这样我们就不必做所有那些乱七八糟......
Let's say my $_POST
variable looks like:
<?php
Array
(
[user_ID] => 65
[action] => editpost
[originalaction] => editpost
[post_author] => 154
[empl_bd_dd] => 6
[empl_bd_mm] => 5
[empl_bd_yy] => 1987
[empl_gen] => 1
[empl_height] => 155
[empl_weight] => 61
[empl_arra] => 2
[save] => Update
[post_it] => 2
[empl_pay] => J77
[empl_cust] => Married
[empl_lang] => Array
(
[0] => EN
[1] => FR
)
[empl_rent] => 1
[name] => Jimmy Nathan
[empl_text] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed interdum leo. Sed et elit quam, tempor placerat neque. Nullam sapien odio, egestas iaculis dictum ut, congue ullamcorper tellus.
[empl_sk_0] => 6
[empl_sk_1] => 7
[empl_sk_2] => 5
)
?>
As you can see I prefixed all my form variables with empl_
. Short of having to specify all of them one by one, how do I get all my form variables from $_POST
into an array in the least-cost hopefully elegant way? Is there a PHP array function or a combination of them that I can use for this?
Like in CSS
where you can select all elements with a class that starts with empl
using [class*="empl_"]
, is there a way I can do this with the array keys in PHP, e.g.
$empl_post = $_POST['empl_*']
EDITED ANSWER - impt correction to @chris 's answer: $_POST
has to be the first argument to array_intersect_key
, e.g.:
$empl_POST = array_intersect_key($_POST, array_flip(preg_grep('/^empl_/', array_keys($_POST))));
$r = array_intersect_key($_POST, array_flip(preg_grep('/^empl_/', array_keys($_POST))));
这篇关于如何获得$ _POST阵列按键的子集开头的preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!