1

I have 3 sets of array's that have been returned back from an API ($array1,$array2,$array3)

I need to combine these into 1 object with the highest price value for 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
        )

)

What's the best way of doing this?

2
  • I think it’s best to do it by hand; just do what you need to do for it, without relying to those dummy PHP functions. array_merge is useful inside the loop. Commented Jan 25, 2016 at 8:01
  • There is also other similar posts... Commented Jan 25, 2016 at 8:02

3 Answers 3

2

A different approach. More functional, simpler and less error prone.

// 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);

Outputs:

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

The good thing about this approach is that it relies on built-in functions and that can easily be extended to more titles and more arrays.

Sign up to request clarification or add additional context in comments.

Comments

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);

3 Comments

Do you know how to make this work with out a preset number of arrays. (Where it could have 2 or 2000).
Can you please specify the input. ? than i could try :)
@RajeshKumar Consider using simpler approach to avoid errors (see my answer)
0

You can your required result as:

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

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

echo "<pre>";
print_r($newArr); // result of array1, you can follow this for other arrays as well
?>

Result:

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

UPDATE 1:

<?
$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);    

?>

Result:

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

4 Comments

What about arr2, arr3?
if other 2 is separate as arr1, than u can follow same process.. @Brad
Alright, these are seperate? and can u add the expected result in question.. @Brad
There are 3 sets off arrays. It's from 3 seperate API calls. I need to find the most expensive of the 3 as I show in the example. The example merges the data from all 3 calls into 1. 2nd day from the 2nd one, and 1 day from the 1st one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.