组合两个数组具有相同的键

时间:2016-04-08 11:13:53

标签: php arrays

我有一个类似

的数组
$sele_itmid = Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 4
    [4] => 4
)
$fltrd_serl_batch = Array
(
    [0] => SAL121
    [1] => SAL122
    [2] => SAL 145
    [3] => M121
    [4] => M122
)

此时我需要将它们组合为$sele_itmid值作为键$fltrd_serl_batch,例如

array(
    [1] => SAL121
    [1] => SAL122
    [1] => SAL 145
    [4] => M121
    [4] => M122
)

我知道无法使用array_combine,因为它不会接受重复的密钥。这样做的另一种方法是什么?有没有任何数组函数可以做到这一点?

1 个答案:

答案 0 :(得分:1)

$itm_btch = array_map(function($key, $val) {return array($key=>$val);}, $sele_itmid, $fltrd_serl_batch);

print_r($itm_btch );
Array
(
    [0] => Array
        (
            [1] => SAL122
        )

    [1] => Array
        (
            [1] => SAL 145
        )

    [2] => Array
        (
            [4] => M121
        )

    [3] => Array
        (
            [4] => M122
        )

)