Home php How to combine two arrays of different length by date PHP

How to combine two arrays of different length by date PHP

Author

Date

Category

Need help, there are 2 arrays.

first array

array (
  [0] = & gt; Array.
    (
      [Countid] = & gt; 4
      [DateCount] = & gt; 2020-09-26
    )
  [1] = & gt; Array.
    (
      [Countid] = & gt; 3.
      [DateCount] = & gt; 2020-09-27
    )
  [2] = & gt; Array.
    (
      [Countid] = & gt; 4
      [DateCount] = & gt; 2020-09-28
    )
  [3] = & gt; Array.
    (
      [Countid] = & gt; 2.
      [DateCount] = & gt; 2020-09-29
    )
  [4] = & gt; Array.
    (
      [Countid] = & gt; fourteen
      [DateCount] = & gt; 2020-09-30
    )
)

second array

array (
  [0] = & gt; Array.
    (
      [Countid2] = & gt; 1
      [DateCount] = & gt; 2020-09-28
    )
  [1] = & gt; Array.
    (
      [Countid2] = & gt; 13
      [DateCount] = & gt; 2020-09-30
    )
)

Task

Combine two array into one by creating a new array so that the output happens so

array (
  [notvisit] = & gt; Array.
    (
      [0] = & gt; 0.
      [1] = & gt; 0.
      [2] = & gt; 1
      [3] = & gt; 0.
      [4] = & gt; 13
    )
  [visits] = & gt; Array.
    (
      [0] = & gt; 4
      [1] = & gt; 3.
      [2] = & gt; 4
      [3] = & gt; 2.
      [4] = & gt; fourteen
    )
  [Date] = & gt; Array.
    (
      [0] = & gt; 26.09
      [1] = & gt; 27.09
      [2] = & gt; 28.09
      [3] = & gt; 29.09
      [4] = & gt; 30.09
    )
)

did so, but the output duplicate dates and data:

$ tmpdata = [];
Foreach ($ TMPDATAS1 AS $ Item) {
  Foreach ($ TMPDATAS2 AS $ Item2) {
  if ($ item ['datecount'] == $ item2 ['datecount']) {
  $ TMPDATA ['notvisit'] [] = $ Item2 ['Countid2'];
  $ TMPDATA ['Visits'] [] = $ item ['Countid'];
  $ TMPDATA ['Date'] [] = date ("D.M", StrTotime ($ item ['datecount']));
  } else {
  $ TMPDATA ['notvisit'] [] = '0';
  $ TMPDATA ['Visits'] [] = $ item ['Countid'];
  $ TMPDATA ['Date'] [] = date ("D.M", StrTotime ($ item ['datecount']));
  }
  }
}

displays such an array, and you need not duplicated

array (
  [notvisit] = & gt; Array.
    (
      [0] = & gt; 0.
      [1] = & gt; 0.
      [2] = & gt; 0.
      [3] = & gt; 0.
      [4] = & gt; 1
      [5] = & gt; 0.
      [6] = & gt; 0.
      [7] = & gt; 0.
      [8] = & gt; 0.
      [9] = & gt; 13
    )
  [visits] = & gt; Array.
    (
      [0] = & gt; 4
      [1] = & gt; 4
      [2] = & gt; 3.
      [3] = & gt; 3.
      [4] = & gt; 4
      [5] = & gt; 4
      [6] = & gt; 2.
      [7] = & gt; 2.
      [8] = & gt; fourteen
      [9] = & gt; fourteen
    )
  [Date] = & gt; Array.
    (
      [0] = & gt; 26.09
      [1] = & gt; 26.09
      [2] = & gt; 27.09
      [3] = & gt; 27.09
      [4] = & gt; 28.09
      [5] = & gt; 28.09
      [6] = & gt; 29.09
      [7] = & gt; 29.09
      [8] = & gt; 30.09
      [9] = & gt; 30.09
    )
)

Answer 1, Authority 100%

$ tmpdatas1 = [['countid' = & gt; '4', 'DateCount' = & gt; '2020-09-26'],
  ['Countid' = & gt; '3', 'DateCount' = & gt; '2020-09-27'],
  ['Countid' = & gt; '4', 'DateCount' = & gt; '2020-09-28'],
  ['Countid' = & gt; '2', 'DateCount' = & gt; '2020-09-29'],
  ['Countid' = & gt; '14', 'DateCount' = & gt; '2020-09-30']];
$ TMPDATAS2 = [['Countid2' = & gt; '1', 'DateCount' = & gt; '2020-09-28'],
  ['Countid2' = & gt; '13', 'DateCount' = & gt; '2020-09-30']];
$ TMPDATA = [];
Foreach ($ TMPDATAS1 AS $ Item) { 
$ val = 0; // Take a variable for each element of the first array
   Foreach ($ TMPDATAS2 AS $ Item2) {
     if ($ item ['datecount'] == $ item2 ['datecount']) {
       $ Val + = $ Item2 ['Countid2']; // if found, we fold the value
     }
   }
   $ TMPDATA ['notvisit'] [] = $ Val; // Display data
   $ TMPDATA ['Visits'] [] = $ item ['Countid'];
   $ TMPDATA ['Date'] [] = date ("D.M", StrTotime ($ item ['datecount']));
}
print_r ($ TMPDATA);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions