1

i have 2 array and i want to merge or combine them...

Array
(
    [0] => Array
        (
            [year] => 2015
            [value] => 32
        )

    [1] => Array
        (
            [year] => 2016
            [value] => 54
        )    
)

Array
(
    [0] => Array
        (
            [year] => 2015
            [value] => 95
        )

    [1] => Array
        (
            [year] => 2016
            [value] => 2068
        )

)

i want them to look like this...

Array(
    [2015]=>array(
        [0] => 32
        [1] => 95
    )
    [2016]=>array(
        [0] => 54
        [1] => 2068
    )
)

it this possible? if ever, how?.... thanks so much

sidyll
  • 57,726
  • 14
  • 108
  • 151
  • Possible duplicate of [PHP - How to merge arrays inside array](http://stackoverflow.com/questions/17041278/php-how-to-merge-arrays-inside-array) – Lekhnath Jul 14 '16 at 12:24
  • 1
    Please give it a try first and share your effort. We're here to help each other learn, not do each other's work. – BeetleJuice Jul 14 '16 at 12:24
  • 1
    You might start by loooking at how you created these 2 arrays in the first place, and create them correctly at that stage rather than fixing them later – RiggsFolly Jul 14 '16 at 12:28

4 Answers4

3
 $a = array(
     0 => array
         (
            "year" => 2015,
            "value" => 32
         ),
     1 => array
         (
            "year" => 2016,
            "value" => 54
         )  
 );

 $b = array(
     0 => array
        (
           "year" => 2015,
           "value" => 300
        ),
    1 => array
       (
           "year" => 2016,
           "value" => 5400
       )  
);

$c = array_merge($a,$b);

$output = array();
foreach($c as $key=>$val)
{
    $output[$val['year']][] = $val['value'];
}

echo '<pre>';
print_r($output);
exit;

Try this code..

Hardik
  • 448
  • 2
  • 9
1

If the original arrays are $a and $b, run this code and the result you want will be in $result

$sources = array_merge($a,$b);
$result = [];
foreach($sources as $data){
    $yr = $data['year'];
    if(!isset($result[$yr])) $result[$yr]=[];
    $result[$yr][]=$data['value'];
}

Live demo

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
0

Try:

$newArr = array();
foreach($array1 as $key1=>$arr1) {
  $newArr[$arr1['year']][] = $arr1['value'];
  $newArr[$arr1['year']][] = $array2[$key]['value'];
}
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • that works only if both arrays are the same size, or and same configuration – Edgars Aivars Jul 14 '16 at 12:29
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 14 '16 at 13:31
0

You can also do something like this,

<?php
$test1 = [["year"=>2015,"value"=>32],["year"=>2016,"value"=>54]];
$test2 = [["year"=>2015,"value"=>95],["year"=>2016,"value"=>2068]];

$newarray=array();
foreach($test1 as $key1=>$value1){
  $temp = [$value1['value']];
  foreach($test2 as $key2=>$value2){
    if($value1['year']==$value2['year']){
    $temp[] = $value2['value'];
    }
    $newarray[$value1['year']] = $temp;
  }
}

print_r($newarray);
?>

check here : https://eval.in/605323

output is :

Array
(
    [2015] => Array
        (
            [0] => 32
            [1] => 95
        )

    [2016] => Array
        (
            [0] => 54
            [1] => 2068
        )

)
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109