-2

I am working on a project where I need to combine multiple arrays to one who has the same date. Let me show you an example

["first"]=>
{
    [0]=>
    {
      ["date"]=> "Jun 14",
      ["hhp_signed"]=> "0"
    }
}

["second"]=>
 {
    [0]=>
    {
      ["date"]=> "Jun 14",
      ["coupon_purchased"]=> 0
    }
 }

["third"]=>
{
    [0]=>
    {
      ["date"]=> "Jun 14",
      ["user_subscription"]=> "0"
    }
}

Here is the expected result

["final"] => {
    [0] => {
    ["date"]=> "Jun 14",
    ["hhp_signed"] => 0,
    ["coupon_purchased"] => 0,
    ["user_subscription"] => 0
    }
    [1] => {
    ["date"]=> "Jun 15",
    ["hhp_signed"] => 2,
    ["coupon_purchased"] => 5,
    ["user_subscription"] => 0
    }
 }

Currently I am writing values of three arrays but there could be more than three arrays may be 7 or 8

I have tried this function but it only works for two arrays, in my case, there would be more than two.

function combo($array1, $array2) {
    $output = array();
    $arrayAB = array_merge($array1, $array2);
    foreach ( $arrayAB as $value ) {
      $id = $value['date'];
      if ( !isset($output[$id]) ) {
        $output[$id] = array();
      }
      $output[$id] = array_merge($output[$id], $value);
    }

    return $output;
}

I really thanks you for your efforts

Neeraj
  • 652
  • 10
  • 18
  • 1
    To ask On Topic question, please read [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [take the tour](http://stackoverflow.com/tour) **We are very willing to help you fix your code, but we dont write code for you** – RiggsFolly Jun 21 '18 at 11:13
  • This is helpful to you: https://stackoverflow.com/questions/17850353/array-merge-on-multidimensional-array – Geee Jun 21 '18 at 11:14
  • Hi Riggs. It's really an unprofessional way to ask something. I just need a function to combine arrays, I want not asking you to write code for me. – Neeraj Jun 21 '18 at 11:16
  • How else are we supposed to answer a question like this without writing code for you. If you gave it a try we would be more than willing to help fix your attempt – RiggsFolly Jun 21 '18 at 11:17
  • Hi RiggsFolly, I edited my question to show you function I am using. Sometimes a developer doesn't know where to start so he needs some inspiration. – Neeraj Jun 21 '18 at 11:26

1 Answers1

0

You need use array_merge function.

Example:

$final = [];
foreach ($original_array as $value)
{
     $final[$value['date']] = array_merge($final, $value);
}
$final['final'] = $final;  // like your example
pablorsk
  • 3,861
  • 1
  • 32
  • 37
  • Hi @pablorsk. Thanks for the answer. I just want to know how can I get the date from my array. I have the structure like this `["first"]=> { [0]=> { ["date"]=> "Jun 14", ["hhp_signed"]=> "0" } }` So you can see that date key have parent key **first**. Thank you again. – Neeraj Jun 21 '18 at 12:03
  • just do `$date = $array['first'][0]['date'];`. This is ok for you? – pablorsk Jun 21 '18 at 12:14