Combine multiple occurences of an array in PHP
Get the solution ↓↓↓so I have an array with data structure like this
$array = [{ ["StartTime"]=> "2020-04-25T08:45:00-07:00", ["Subject"]=> "Beach Walk" }, { ["StartTime"]=> "2020-04-27T08:45:00-07:00", ["Subject"]=> "Beach Walk" }, { ["StartTime"]=> "2020-04-27T08:45:00-07:00", ["Subject"]=> "Camping Techniques"} ]
];
I want to create a new array in PHP that combines the instances with the same subject and have all the times put together Desired Output:
$DesireArray = [{ ["StartTimes"]=> "2020-04-25T08:45:00-07:00", "2020-04-27T08:45:00-07:00",["Subject"]=> "Beach Walk" }, { ["StartTimes"]=> "2020-04-27T08:45:00-07:00", ["Subject"]=> "Camping Techniques"} ]
];
I have been trying a few functions but none bring me my desired result. Any help would be much appreciated!
Answer
Solution:
An solution with only one loop,
$array = [
[
"StartTime" => "2020-04-25T08:45:00-07:00",
"Subject" => "Beach Walk",
],
[
"StartTime" => "2020-04-27T08:45:00-07:00",
"Subject" => "Beach Walk"
],
[
"StartTime" => "2020-04-27T08:45:00-07:00",
"Subject" => "Camping Techniques",
],
];
$out = [];
foreach ($array as $key => $x) {
$sub = $x['Subject'];
$out[$sub]['StartTimes'][] = $x['StartTime'];
$out[$sub]['Subject'] = $x['Subject'];
}
$final = array_values($out);
print_r($final);
// output:
/**
Array
(
[0] => Array
(
[StartTimes] => Array
(
[0] => 2020-04-25T08:45:00-07:00
[1] => 2020-04-27T08:45:00-07:00
)
[Subject] => Beach Walk
)
[1] => Array
(
[StartTimes] => Array
(
[0] => 2020-04-27T08:45:00-07:00
)
[Subject] => Camping Techniques
)
)
**/
Answer
Solution:
The idea is first to read original array and reshuffle elements as you want:
$array = [[ "StartTime"=> "2020-04-25T08:45:00-07:00", "Subject"=> "Beach Walk" ], [ "StartTime"=> "2020-04-27T08:45:00-07:00", "Subject"=> "Beach Walk" ], [ "StartTime"=> "2020-04-27T08:45:00-07:00", "Subject"=> "Camping Techniques"] ];
print_r($array);echo "<BR><BR>";
$arrayStartTimes = array();
$arraySubject = array();
foreach ($array as $arrayAux){
foreach ($arrayAux as $key=>$value){
if ($key == "StartTime") $startTime = $value;
if ($key == "Subject") {
$arrayStartTimes[$value][] = $startTime;
if (!in_array($value,$arraySubject)) $arraySubject[] = $value;
}
}
}
$DesiredArrayNew = array();
foreach ($arraySubject as $iSubject){
$auxArray = array();
$auxArray2 = array();
echo "$iSubject: ";
foreach ($arrayStartTimes[$iSubject] as $iStart){
echo " $iStart,";
$auxArray2[] = $iStart;
}
echo "<BR>";
$auxArray["StartTimes"]=$auxArray2;
$auxArray["Subject"]=$iSubject;
$DesiredArrayNew[] = $auxArray;
}
echo "<BR><BR>";
$DesireArray = [["StartTimes"=> ["2020-04-25T08:45:00-07:00", "2020-04-27T08:45:00-07:00"],"Subject"=> "Beach Walk" ], [ "StartTimes"=> ["2020-04-27T08:45:00-07:00"], "Subject"=> "Camping Techniques"]];
print_r($DesireArray);
echo "<BR><BR>";
print_r($DesiredArrayNew);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: port 80 in use by "unable to open process" with pid 4!
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.