I have the following array (array is large):
Array (
[0] => Array (
[id] => 1
[timestamp] => 1503050400
[name] => Event A
[value] => )
[1] => Array (
[id] => 2
[timestamp] => 1503446400
[name] => Event B
[value] => )
[2] => Array (
[id] => 2
[timestamp] => 1503446400
[name] => Event B
[value] => 71 )
[3] => Array (
[id] => 3
[timestamp] => 1503720000
[name] => Event C
[value] => 12 )
[4] => Array (
[id] => 3
[timestamp] => 1503720000
[name] => Event C
[value] => )
...
)
As you can see, some array keys (rows) have same ID, Timestamp, and Name, but different Value. I would like to find and unset($array[$key]) rows which meet the following conditions:
if array has keys with the same Name, Id and Timestamp, delete this keys, but leave only with Value != null
Looking something like this:
foreach ($array as $key => $row) {
if ( ... ) {
unset($array[$key]);
}
else {}
}
print_r($array);
The output should be:
Array (
[0] => Array (
[id] => 1
[timestamp] => 1503050400
[name] => Event A
[value] => )
[2] => Array (
[id] => 2
[timestamp] => 1503446400
[name] => Event B
[value] => 71 )
[3] => Array (
[id] => 3
[timestamp] => 1503720000
[name] => Event C
[value] => 12 )
...
)
You can use
array_reduce()
andarray_filter()
:For reference, see:
For an example, see: