how to split one array in to multiple arrays based on clientID in php
Get the solution ↓↓↓I am trying to split the array and create for each clienID one new array. I have not yet manage how to split the array to two new arrays (example has two clientIDs). All rows with the same clientID should be in one array:
We can see that I have two clientIDs'51df'
and'51ff'
File with raw data:
clientID,Timestamp, Room temp., Pressure, Humidity, PM1.0, PM2.5
51df,05/04/2021 11:20:42,22.28,988.61,37.97,0,0
51ff,05/04/2021 11:20:53,22.29,988.57,37.95,0,0
51df,05/04/2021 11:21:05,22.29,988.55,37.94,0,0
51ff,05/04/2021 11:21:16,22.29,988.63,37.95,0,0
51df,05/04/2021 11:21:27,22.28,988.57,37.97,1,1
51ff,05/04/2021 11:21:38,22.30,988.61,37.95,1,1
51df,05/04/2021 11:21:50,22.30,988.74,37.94,0,0
...
array(24) { [0]=> array(7)
{ [0]=> string(2) "ID" [1]=> string(9) "Timestamp" [2]=> string(16) "Room temperature" [3]=> string(8) "Pressure" [4]=> string(8) "Humidity" [5]=> string(5) "PM2.5" [6]=> string(5) "PM1.0" } [1]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:20:42" [2]=> string(5) "22.28" [3]=> string(6) "988.61" [4]=> string(5) "37.97" [5]=> string(1) "0" [6]=> string(1) "0" } [2]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:20:53" [2]=> string(5) "22.29" [3]=> string(6) "988.57" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [3]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:21:05" [2]=> string(5) "22.29" [3]=> string(6) "988.55" [4]=> string(5) "37.94" [5]=> string(1) "0" [6]=> string(1) "0" } [4]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:21:16" [2]=> string(5) "22.29" [3]=> string(6) "988.63" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [5]=>
...
What I have until now. I am reading all the file into an array and do the transition to split each row at ',', which gives me the above printout:
<?php
$latestFile = '/client/logs/logging_multipleClients.out';
$tmp = tail($latestFile, $lines = 400, $buffer = 4096);
$tmp1 = preg_split( "/\n/", $tmp );
$data = array();
$table = 'ID,Timestamp,Room temperature,Pressure,Humidity,PM2.5,PM1.0';
$tmp2 = preg_split( "/\,/", $table );
array_push($data, $tmp2);
for ($j=1; $j < count($tmp1); $j++) {
$tmp2 = preg_split( "/\,/", $tmp1[$j] );
array_push($data, $tmp2);
}
var_dump($data);
?>
How do I now split this$data
array into two arrays according to the clientIDs?
So, at the end I would like to have:
client['0'] with all the row with clientIDs '51df'
client['1'] with all the row with clientIDs '51ff'
After I manage that it should be rather easy to split the$data
array into as many clientIDs the file could have.
Would it be easier if I would read the file line by line, something like this:
if (($handle = fopen($latestFile, "r")) !== FALSE) {
$data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// do something here to push each line to the correspondent client array according to the clientID.
}
}
Thanks
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update.
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.