Merging PHP Array - php

I have two arrays:
array1:
Array ( [id] => 1 [time] => 12:10:23 [date] => 2013-03-24 )
array2:
Array ( [id] => 2 [time] => 12:10:25 [date] => 2013-03-25 )
I would like to merge them so they result in the following (EDITED):
Array ( [id] => 1
=> 2
[time] => 12:10:23
=> 12:10:25
[date] => 2013-03-24
=> 2013-03-25
)
Is this possible?

If you actually mean to have the output:
Array (
Array ([id] => 1 [time] => 12:10:23 [date] => 2013-03-24 ),
Array ([id] => 2 [time] => 12:10:25 [date] => 2013-03-25 )
)
Then you want to use the following code:
$newArray = array($array1, $array2);
To get the element with the most recent date:
$most_recent = $newArray[0];
foreach($newArray as $compare)
{
if($compare['time'] > $most_recent['time'])
{
$most_recent = $compare;
}
}

Related

Combining two multidimensional arrays taking structure of one and content of the other in php

I'm somewhat new with PHP. Is there a way to merge the two following arrays so that the [2181] and [2180] keys (and all of their content) in the second array can replace the two datetime objects in the first array while still maintaining the structure of the first array? The datetime objects from the first array will have the same [date] key value as the [date] key value from the second array at the [2182] and [2180] index, although I'm not sure if this shared key=>value will be necessary to merge the arrays correctly.
1.) Array
(
[0] => Array
(
[0] => DateTime Object
(
[date] => 2017-04-23 13:35:17.000000
[timezone_type] => 3
[timezone] => America/Denver
)
)
[1] => Array
(
[0] => DateTime Object
(
[date] => 2017-04-22 13:35:17.000000
[timezone_type] => 3
[timezone] => America/Denver
)
)
2.) Array
(
[2182] => Array
(
[id] => 2182
[date] => 2017-04-23
[door] => Array
(
[1] => Array
(
[id] => 2999
)
)
)
[2180] => Array
(
[id] => 2180
[date] => 2017-04-22
[door] => Array
(
[1] => Array
(
[id] => 2994
)
)
)
This final array is how I need it to be structured.
3.) Array
(
[0] => Array
(
[2182] => Array
(
[id] => 2182
[date] => 2017-04-23
[door] => Array
(
[1] => Array
(
[id] => 2999
)
)
)
)
[1] => Array
(
[2180] => Array
(
[id] => 2180
[date] => 2017-04-22
[door] => Array
(
[1] => Array
(
[id] => 2994
)
)
)
)
Thanks in advance!
I don't understand what are you exactly asking.
Given the final array as you want in your question, you can do like this
<?php
$date = [[0=>new dateTime()], [new dateTime()]];// why this array if you are not using..
$arr = [
2182=>["id"=>2182,"date"=>"2017-04-5", "door"=>[1=>["id"=>2999]]],
2180=>["id"=>2180,"date"=>"2017-04-22", "door"=>[1=>["id"=>2994]]]
];
print_r(($arr));
$newArr = [];
foreach($arr as $a){
$newArr[] = $a;
}
print_r($newArr);
Tested online

Grouping arrays by specified key value pair

Heys guys I have the following array:
Array
(
[0] => Array
(
[date] => 2017-03-31
[1] => 7.9950
)
[1] => Array
(
[date] => 2017-06-30
[1] => 8.3425
)
[2] => Array
(
[date] => 2017-03-31
[2] => 6.8250
)
[3] => Array
(
[date] => 2017-06-30
[2] => 10.7725
)
[4] => Array
(
[date] => 2017-03-31
[3] => 6.4950
)
[5] => Array
(
[date] => 2017-06-30
[3] => 7.3425
)
)
I need to merge all the values that share the same date into one array, preserving the original keys if possible. They're id's that I'll then json_encode to build a chart.
Array
(
[0] => Array
(
[date] => 2017-03-31
[1] => 7.9950
[2] => 6.8250
[3] => 6.4950
)
[1] => Array
(
[date] => 2017-06-30
[1] => 8.3425
[2] => 10.7725
[3] => 7.3425
)
)
Is there a way of achieving this? I've looked for similar questions, searched the php manual for functions like array_merge and alike, but couldn't achieve the desired result. Any help would be appreciated.
How about something like this:
$output = array();
foreach($input as $record){
foreach($record as $key => $value){
$output[$record['date']][$key] = $value;
}
}
//make it a regular array
$output = array_values($output);
Any duplicate data will appear only once.

Sorting and merge associative array (PHP)

I have 2 associative arrays:
[0] => Array (
[time] => 09:00:00
[name] => To do something 1
)
[1] => Array (
[time] => 09:30:00
[name] => To do something 2
)
[2] => Array (
[time] => 10:00:00
[name] => To do something 3
)
and
[0] => Array (
[time] => 09:00:00
)
[1] => Array (
[time] => 09:15:00
)
[2] => Array (
[time] => 09:30:00
)
[3] => Array (
[time] => 09:45:00
)
[4] => Array (
[time] => 10:00:00
)
Need to do another associative array:
if the field [time] in second array equals field [time] in first array then add [name];
if field [time] in second array not equals field [time] in first array then add empty [name].
Should look like this:
[0] => Array (
[time] => 09:00:00
[name] => To do something 1
)
[1] => Array (
[time] => 09:15:00
[name] =>
)
[2] => Array (
[time] => 09:30:00
[name] => To do something 2
)
[3] => Array (
[time] => 09:45:00
[name] =>
)
[4] => Array (
[time] => 10:00:00
[name] => To do something 3
)
After some puzzling I made it work for you.
This is the code you can use:
$array3 = array();
foreach ($array2 as $key2 => $value2){
$found = false;
foreach ($array1 as $key1 => $value1){
if ($array1[$key2]['time'] == $array2[$key2]['time']) {
$array3[$key2]['time'] = $array1[$key1]['time'];
$array3[$key2]['name'] = $array1[$key1]['name'];
$found = true;
}
}
if (!$found){
//add
$array3[$key2]['time'] = $array2[$key2]['time'];
$array3[$key2]['name'] = "";
}
}
print_r($array3);

Merge these array values into a new array?

I have the following array stored in $data
Array
(
[name] => JimBob
[data] => Array
(
[0] => Array
(
[date] => 2013-15-5
[pole] => 2
[race] => 43
)
[1] => Array
(
[date] => 2013-15-6
[pole] => 15
[race] => 34
)
[2] => Array
(
[date] => 2013-15-7
[pole] => 9
[race] => 54
)
)
)
I need to create a new array which looks like this
Array
(
[name] => JimBob
[data] => Array
(
[0] => 2013-15-5,2
[1] => 2013-15-6,15
[2] => 2013-15-7,9
)
)
So far I have this array_map function
$arr2['data'] = array_map(function($date) {
return $date['date'];
}, $data['data']);
print_r($arr2);
Which outputs the below, how can I get the pole values added to these dates separated by comma?
Array
(
[data] => Array
(
[0] => 2013-15-5
[1] => 2013-15-6
[2] => 2013-15-7
)
)
Use concatenation.
$arr2['data'] = array_map(function($date) {
return $date['date'] . ',' . $date['pole'];
}, $data['data']);

php compare todays date with dates in (multi-dimensional) array?

I have an array of dates, I was wondering if it was possible to search an array and remove any items which are BEFORE todays date, and keep the rest?
Here's my array
Array
(
[0] => Array
(
[date] => 2013-07-14
)
[1] => Array
(
[date] => 2013-08-31
)
[2] => Array
(
[date] => 2013-09-15
)
[3] => Array
(
[date] => 2013-10-12
)
[4] => Array
(
[date] => 2013-10-16
)
[5] => Array
(
[date] => 2013-10-19
)
[6] => Array
(
[date] => 2013-10-23
)
[7] => Array
(
[date] => 2013-10-26
)
[8] => Array
(
[date] => 2013-10-30
)
[9] => Array
(
[date] => 2013-09-07
)
[10] => Array
(
[date] => 2013-08-14
)
[11] => Array
(
[date] => 2013-08-24
)
[12] => Array
(
[date] => 2013-09-11
)
[13] => Array
(
[date] => 2013-09-28
)
[14] => Array
(
[date] => 2013-10-05
)
)
Something like that:
$buffer = array();
foreach ($dates as $element) {
if (strtotime($element['date']) >= time()) {
$buffer[] = $element;
}
}
What about looping throgh and comparing dates with actual required data and build a new filtered array?
$newarray = array();
foraeach($array as $data)
{
if($data['date'] >= $yourdate)
{
$newarray] = $data;
}
}
array_walk function lets you define a callback to run on every item in an array.

Categories