在PHP中打破$ _POST数组

时间:2012-06-13 11:28:53

标签: php arrays

我有一个$_POST数组,如下所示

Array
(
    [questionTitle1] => question one
    [questionNote1] => note one
    [oprtionValue11] => green 1
    [oprtionValue21] => blue 1
    [oprtionValue31] => orange 1
    [questionTitle2] => question two
    [questionNote2] => note two
    [oprtionValue5] => green 2
    [oprtionValue6] => blue 2
    [oprtionValue7] => orange 2
    [oprtionValue8] => red 2
) 

这里我想把数组分成单独的数组,比如当出现像questionTitle%这样的$ key时。

1 个答案:

答案 0 :(得分:3)

试试这个

$filteredArray = array();
$indexPattern = '/questionTitle(.*)/';
foreach($_POST as $key => $value) {
    if(preg_match($indexPattern, $key)) {
        $filteredArray[$key] = $value;
    }
}