0

I have two arrays with datetimes. I want to display the values in the x-axis of a chart.

I need a function that combine the arrays in one and add a '0' where aren't duplicates.

array 1 = [2016-01-20,2016-01-21,2016-01-24]
array 2 = [2016-01-21]

final array = [0, 2016-01-21, 0]

Is there any quick way to this?

thank you very much

Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
Tudor-Radu Barbu
  • 444
  • 1
  • 11
  • 28

2 Answers2

1

Just loop through first array and check if value is in second array.

foreach ($array1 as &$date) {
    if (!in_array($date, $array2)) {
        $date = 0;
    }
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

You can do this with map() and indexOf()

var array1 = ['2016-01-20', '2016-01-21', '2016-01-24']
var array2 = ['2016-01-21']

var final = array1.map(function(e) {
  return (array2.indexOf(e) == -1) ? e = 0 : e;
});

console.log(final)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176