PHP多维数组合并

时间:2016-01-25 07:15:04

标签: php arrays

我有3组已经从API返回的数组($ array1,$ array2,$ array3)

我需要将这些组合成一个具有最高价格值的对象,用于Ground,2nd Day,Overnight。

$obj['ground'] should equal 3.15
$obj['2 Day'] should equal 19.29
$obj['Overnight'] should equal 29.26

ARR1

Array
(
    [0] => Array
        (
            [Title] => Ground
            [Price] => 3.15
        )

    [1] => Array
        (
            [Title] => 2 Day
            [Price] => 12.11
        )

    [2] => Array
        (
            [Title] => 1 Day
            [Price] => 29.26
        )

)

ARR2

Array
(
    [0] => Array
        (
            [Title] => Ground
            [Price] => 3.15
        )

    [1] => Array
        (
            [Title] => 2 Day
            [Price] => 19.29
        )

    [2] => Array
        (
            [Title] => 1 Day
            [Price] => 25.89
        )

)

ARR3

Array
(
    [0] => Array
        (
            [Title] => Ground
            [Price] => 3.15
        )

    [1] => Array
        (
            [Title] => 2 Day
            [Price] => 16.29
        )

    [2] => Array
        (
            [Title] => 1 Day
            [Price] => 25.89
        )

)

这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:2)

        $array1 = array(
        array("Title"=>"ground", "Price"=>3.15),
        array("Title"=>"2 Day", "Price"=>12.11),
        array("Title"=>"1 Day", "Price"=>29.26)
        );
    $array2 = array(
        array("Title"=>"ground", "Price"=>3.15),
        array("Title"=>"2 Day", "Price"=>19.29),
        array("Title"=>"1 Day", "Price"=>25.89)
    );
    $array3 = array(
        array("Title"=>"ground", "Price"=>3.15),
        array("Title"=>"2 Day", "Price"=>16.29),
        array("Title"=>"1 Day", "Price"=>25.89)
    );
    $obj = new stdClass(); /** you asked object. */
    $size = count($array1);
/** Loops runs three time, `ground`, `2 Day` and `1 Day` will be sorted respectively in each iteration */
    for ($i = 0; $i<$size; $i++) {
        $maxValue = max(array($array1[$i]["Price"], $array2[$i]["Price"], $array3[$i]["Price"]));
        switch ($array1[$i]["Title"]) {
            case "ground": $obj->ground = $maxValue;
                break;
            /** '2 day' is not possible since object properties can not contain space. */
            case "2 Day": $obj->twoDay = $maxValue;
                break;
            case "1 Day": $obj->overNight = $maxValue;
                break;
        }
    }
    var_dump($obj);

答案 1 :(得分:2)

一种不同的方法。功能更多,更简单,更不容易出错。

// Prepare result
$result = array_fill_keys([ 'ground', '2 Day', '1 Day' ], 0.0);

// Find the highest price and keep it in the result
$result = array_reduce(array_merge($array1, $array2, $array3), function ($result, $item) {
    if ($item['Price'] > $result[$item['Title']]) {
        $result[$item['Title']] = $item['Price'];
    }
    return $result;
}, $result);

print_r($result);

输出:

Array
(
    [ground] => 3.15
    [2 Day] => 19.29
    [1 Day] => 29.26
)

这种方法的好处在于它依赖于内置函数,并且可以轻松扩展到更多标题和更多数组。

答案 2 :(得分:0)

您可以将所需的结果显示为:

Array
(
    [Ground] => 3.15
    [2 Day] => 12.11
    [1 Day] => 29.26
)

<强>结果:

<?
$arr1[] = array('Title'=>'Ground','Price'=>'3.15');
$arr1[] = array('Title'=>'2 Day','Price'=>'12.11');
$arr1[] = array('Title'=>'1 Day','Price'=>'29.26');

$arr2[] = array('Title'=>'Ground','Price'=>'3.15');
$arr2[] = array('Title'=>'2 Day','Price'=>'19.29');
$arr2[] = array('Title'=>'1 Day','Price'=>'25.89');

$arr3[] = array('Title'=>'Ground','Price'=>'3.15');
$arr3[] = array('Title'=>'2 Day','Price'=>'16.29');
$arr3[] = array('Title'=>'1 Day','Price'=>'25.89');

$customArr = array();
foreach ($arr1 as $key => $value) {
    $customArr[$key][$value['Title']][] = $value['Price'];    
}

foreach ($arr2 as $key => $value) {
    $customArr[$key][$value['Title']][] = $value['Price'];    
}

foreach ($arr3 as $key => $value) {
    $customArr[$key][$value['Title']][] = $value['Price'];    
}

$finalArr = array();
foreach ($customArr as $key => $value) {

    if(isset($value['Ground']))
        $finalArr['Ground'] = max($value['Ground']);

    if(isset($value['2 Day']))
        $finalArr['2 Day'] = max($value['2 Day']);

    if(isset($value['1 Day']))
        $finalArr['1 Day'] = max($value['1 Day']);
}

echo "<pre>";
print_r($finalArr);    

?>

更新1:

Array
(
    [Ground] => 3.15
    [2 Day] => 19.29
    [1 Day] => 29.26
)

<强>结果:

PS>.\adb.exe pull '/sdcard/temp/screenshot.png'
.\adb.exe : 6040 KB/s (34027 bytes in 0.005s)
At line:1 char:1
+ .\adb.exe pull '/sdcard/temp/screenshot.png'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (6040 KB/s (34027 bytes in 0.005s):String) [ 
   ], RemoteException
    + FullyQualifiedErrorId : NativeCommandError