I am trying to split an array of space-delimited strings, group by a particular column, then store the data within each group in a more convenient structure.
Sample data:
$dataArray = [
0 => "AAAAA 2023 01 25 01:04:00 ID:20fjrjeZZ",
1 => "AAAAA 2023 01 25 01:18:08 ID:13454B43A",
2 => "AAAAA 2023 01 25 02:00:02 ID:18f5hjeWe",
3 => "AAAAA 2023 01 25 04:10:13 ID:13454B43A",
4 => "BBBBB 2023 01 25 01:44:10 ID:Xj74320fj",
5 => "BBBBB 2023 01 25 07:08:58 ID:13454B43A",
6 => "BBBBB 2023 01 25 08:40:52 ID:Ftzkk800Y",
7 => "BBBBB 2023 01 25 14:10:13 ID:18f5hjeWe"
];
I split the rows on the space with:
$lines = explode(' ', $dataArray);
Then I want to push the individual parts (AAAA, 2023, 01, ...) into an array.
foreach($dataArray as $parts){
$spotArray[] = $parts[$parts][0];
$yearArray[] = $parts[$parts][1];
// ...
}
Then build a new structure with the new array parts:
foreach($dataArray as $key => $value){
$desiredArray[] = $spotArray[["user"[$yearArray[$hourArray]]], "first"[/** ... */]];
//...
}
Expected result:
$desiredArray = [
"AAAAA" => [
"user" => [
"ID:20fjrjeZZ" => ["01:04:00"],
"ID:13454B43A" => ["01:18:08", "04:10:12"],
"ID:18f5hjeWe" => ["02:00:02"]
],
"first" => "01:04:00",
"last" => "04:10:12",
"totaUser" => 3,
"totalAccess" => 4
],
"BBBBB" => [
"user" => [
"ID:Xj74320fj" => ["01:44:10"],
"ID:13454B43A" => ["07:08:58"],
"ID:Ftzkk800Y" => ["08:40:52"],
"ID:18f5hjeWe" => ["14:10:13"]
],
"first" => "01:44:10",
"last" => "14:10:13",
"totaUser" => 4,
"totalAccess" => 4
]
];
It is not at all necessary to run two loops.
Parse the space-delimited strings in your array and build/overwrite/sum as you iterate.
Code: (Demo)
$result = [];
foreach ($dataArray as $row) {
[$group, $y, $m, $d, $t, $id] = explode(' ', $row);
$result[$group]['user'][$id][] = $t; // accumulate nested elements
$result[$group]['first'] ??= $t; // only store the first occurrence
$result[$group]['last'] = $t; // keep overwriting each time
$result[$group]['totaluser'] = count($result[$group]['user']); // count what is accumulated
$result[$group]['totalAccess'] = ($result[$group]['totalAccess'] ?? 0) + 1; // increment
}
var_export($result);
You can even safely remove the unused $y
, $m
, and $d
declarations if you wish. (Demo)
Output (from either snippet)
array (
'AAAAA' =>
array (
'user' =>
array (
'ID:20fjrjeZZ' =>
array (
0 => '01:04:00',
),
'ID:13454B43A' =>
array (
0 => '01:18:08',
1 => '04:10:13',
),
'ID:18f5hjeWe' =>
array (
0 => '02:00:02',
),
),
'first' => '01:04:00',
'last' => '04:10:13',
'totaluser' => 3,
'totalAccess' => 4,
),
'BBBBB' =>
array (
'user' =>
array (
'ID:Xj74320fj' =>
array (
0 => '01:44:10',
),
'ID:13454B43A' =>
array (
0 => '07:08:58',
),
'ID:Ftzkk800Y' =>
array (
0 => '08:40:52',
),
'ID:18f5hjeWe' =>
array (
0 => '14:10:13',
),
),
'first' => '01:44:10',
'last' => '14:10:13',
'totaluser' => 4,
'totalAccess' => 4,
),
)