I have a text flat file data.txt with data in this specific format and it's always the same:
id: 24153
firstname: john
lastname: smith
registered: true
id: 27663
firstname: ben
lastname: jackson
registered: false
How can I read the text file and loop through all the data points to make a single array with all the data arranged in the following way:
Array
(
[0] => Array
(
[id] => 24153
[firstname] => john
[lastname] => smith
[registered] => 1
)
[1] => Array
(
[id] => 27663
[firstname] => ben
[lastname] => jackson
[registered] =>
)
)
My incomplete/failed attempts. I can put it all into a single array but not sure how to format it in the way described.
$lines = file('data.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$arr = array();
foreach ($lines as $line_num => $line) {
if (strpos($line, 'id') !== false) {
$arr[$line] = //not sure how to move forward from here
}
}
file()to read the entire file into an array. From there you might be interested inarray_chunk()to break up the array into groups of 4. Then you might want to look atarray_map()andarray_reduce()for transforming the data. Let us know how you go