PHP - 合并两个类似于array_combine但具有重复键的数组

时间:2022-06-27 09:08:26

I have two arrays:

我有两个数组:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');

I would like to merge them into something like this

我想把它们合并成这样的东西

$arrResult = array(
    array('str' => 1.22),
    array('str' => 1.99),
    array('otherStr' => 5.17)
);

The keys are non-unique, otherwise I'd use array_combine. That would give a bit different output, but it would suit me as well.

键是非唯一的,否则我会使用array_combine。这会产生不同的输出,但它也适合我。

Can this be done in an elegant way using PHP 5.2.x, without foreach/for loops, preferably using PHP's built-in functions?

这可以使用PHP 5.2.x以优雅的方式完成,没有foreach / for循环,最好使用PHP的内置函数吗?

3 个解决方案

#1


12  

You can use array_map:

你可以使用array_map:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');
function foo($key, $val) {
   return array($key=>$val);
}

$arrResult = array_map('foo', $arrKeys, $arrVals);

print_r($arrResult);
Array
(
    [0] => Array
        (
            [str] => 1.22
        )

    [1] => Array
        (
            [str] => 1.99
        )

    [2] => Array
        (
            [otherStr] => 5.17
        )

)

BTW, if you upgrade to PHP 5.3 you can do this using an anonymous function, which is a bit more elegant:

顺便说一句,如果你升级到PHP 5.3,你可以使用匿名函数来做到这一点,它更优雅:

array_map(function($key, $val) {return array($key=>$val);}, $arrKeys, $arrVals);

#2


9  

There is absolutely nothing "elegant" in using PHP's built-in functions and nothing to pursue for.

使用PHP的内置函数绝对没有什么“优雅”,没有什么可追求的。

This is not a Haute Couture show. This is programming.
As long as you have a solution that is sane, readable and works, you can call it elegant.

这不是高级时装秀。这是编程。只要你有一个理智,可读和有效的解决方案,你就可以称之为优雅。

Simple foreach will do the job. You'd do yourself huge favor and save a lot of time if start using working solutions instead of looking for "elegant" ones.

简单的foreach将完成这项工作。如果开始使用工作解决方案而不是寻找“优雅”的解决方案,那么你会给自己带来巨大的好处并节省大量时间。

$result = array();
foreach ($arrKeys as $i => $key) {
  $result[] = array($key => $arrVals[$i]);
}

#3


2  

Assuming you want item 0 in $arrKeys and item 0 in $arrVals to be added to one array, and that array to be an item in $arrResult, and so on, the easiest way would probably be a simple loop:

假设您希望将$ arrKeys中的项0和$ arrVals中的项0添加到一个数组中,并将该数组作为$ arrResult中的项,依此类推,最简单的方法可能是一个简单的循环:

$arrResult = array();
for ($i = 0, $max = count($arrKeys); $i < $max; $i++) {
    $arrResult[] = array($arrKeys[$i], $arrValues[$i]);
}

#1


12  

You can use array_map:

你可以使用array_map:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');
function foo($key, $val) {
   return array($key=>$val);
}

$arrResult = array_map('foo', $arrKeys, $arrVals);

print_r($arrResult);
Array
(
    [0] => Array
        (
            [str] => 1.22
        )

    [1] => Array
        (
            [str] => 1.99
        )

    [2] => Array
        (
            [otherStr] => 5.17
        )

)

BTW, if you upgrade to PHP 5.3 you can do this using an anonymous function, which is a bit more elegant:

顺便说一句,如果你升级到PHP 5.3,你可以使用匿名函数来做到这一点,它更优雅:

array_map(function($key, $val) {return array($key=>$val);}, $arrKeys, $arrVals);

#2


9  

There is absolutely nothing "elegant" in using PHP's built-in functions and nothing to pursue for.

使用PHP的内置函数绝对没有什么“优雅”,没有什么可追求的。

This is not a Haute Couture show. This is programming.
As long as you have a solution that is sane, readable and works, you can call it elegant.

这不是高级时装秀。这是编程。只要你有一个理智,可读和有效的解决方案,你就可以称之为优雅。

Simple foreach will do the job. You'd do yourself huge favor and save a lot of time if start using working solutions instead of looking for "elegant" ones.

简单的foreach将完成这项工作。如果开始使用工作解决方案而不是寻找“优雅”的解决方案,那么你会给自己带来巨大的好处并节省大量时间。

$result = array();
foreach ($arrKeys as $i => $key) {
  $result[] = array($key => $arrVals[$i]);
}

#3


2  

Assuming you want item 0 in $arrKeys and item 0 in $arrVals to be added to one array, and that array to be an item in $arrResult, and so on, the easiest way would probably be a simple loop:

假设您希望将$ arrKeys中的项0和$ arrVals中的项0添加到一个数组中,并将该数组作为$ arrResult中的项,依此类推,最简单的方法可能是一个简单的循环:

$arrResult = array();
for ($i = 0, $max = count($arrKeys); $i < $max; $i++) {
    $arrResult[] = array($arrKeys[$i], $arrValues[$i]);
}