I get an array with file ('file.txt')
, a file like this:
"Monday 21 August
...
...
Tuesday
...
...
Wednesday 23 August
... "
I need to split the array by day of the week (regex array):
$ days = array (
'/ ^ Monday + /',
'/ ^ Tuesday + /',
'/ ^ Wednesday + /',
'/ ^ Thursday + /',
'/ ^ Friday + /',
'/ ^ Saturday + /',
'/ ^ Sunday + /',
);
If there are 7 days, then file () will turn into 7 arrays. I can find the keys of the days of the week using preg_grep, which occurs in file () … If you had to make 2 arrays from one, you could use array_slice. But what if there can be 7 or more of them?
Answer 1, authority 100%
Alternatively, you can bring it to perfection yourself:
& lt;? php
$ array [0] = 'Monday day';
$ array [1] = '12';
$ array [2] = '12';
$ array [3] = 'Tuesday day';
$ array [4] = '45';
$ array [5] = '45';
$ array [6] = 'Wednesday afternoon';
$ array [7] = '789';
$ array [8] = '789';
$ array [9] = '789';
$ now = -1;
$ response = array ();
foreach ($ array as $ key = & gt; $ value) {
if (preg_match ('/ ^ Monday + | ^ Tuesday + | ^ Wednesday + /', $ value)) {
$ now ++;
} else {
if ($ now & gt; -1)
$ response [$ now] [] = $ value;
}
}
print_r ($ response);
Then figure it out for yourself, the answer is:
Array
(
[0] = & gt; Array
(
[0] = & gt; 12
[1] = & gt; 12
)
[1] = & gt; Array
(
[0] = & gt; 45
[1] = & gt; 45
)
[2] = & gt; Array
(
[0] = & gt; 789
[1] = & gt; 789
[2] = & gt; 789
)
)
Answer 2
Greetings! Perhaps the solution is not the most elegant, if anything, modify it yourself! 😉
Text in file:
Monday, August 21
Server is running
Tuesday 22 August
Server crashed
Wednesday 23 August
Server is running
Server is running
Wednesday 24 August
Server crashed
Tuesday 25 August
$ text_arr = file ("t.txt");
$ days = [
'/ ^ Monday + /',
'/ ^ Tuesday + /',
'/ ^ Wednesday + /',
'/ ^ Thursday + /',
'/ ^ Friday + /',
'/ ^ Saturday + /',
'/ ^ Sunday + /',
];
$ keys = [];
$ result = [];
/ **
* We iterate over the array of patterns and check the occurrences in the text
* cutting off empty elements, and write in $ keys the ordinal number of the line with the date
* All subsequent id between them, our future arrays with text.
* At the output, we get an array of the form:
* [0] = & gt; int (0) [1] = & gt; int (2) [2] = & gt; int (9) [3] = & gt; int (4) [4] = & gt; int (7) ...
* /
foreach ($ days as $ day):
$ search = preg_grep ($ day, $ text_arr);
if (! empty ($ search)):
foreach ($ search as $ id = & gt; $ chunk):
array_push ($ keys, $ id);
endforeach;
else:
continue;
endif;
endforeach;
/ ** Sort the array by values, in order, since we will parse the text from top to bottom * /
sort ($ keys);
/ **
* Increase our id by 1 in order not to include dates in the result
* Trim the array by indentation, where the starting indent will be the id found above,
* and the length of the indentation will be the difference between the id of the next element and the current one.
* /
foreach ($ keys as $ id = & gt; $ key):
++ $ key;
if (++ $ id! = count ($ keys)):
array_push ($ result, array_slice ($ text_arr, $ key, next ($ keys) - $ key));
else:
array_push ($ result, array_slice ($ text_arr, $ key));
endif;
endforeach;
Result:
array (5) {
[0] = & gt;
array (1) {
[0] = & gt;
string (31) "Server is running"
}
[1] = & gt;
array (1) {
[0] = & gt;
string (23) "Server crashed"
}
[2] = & gt;
array (2) {
[0] = & gt;
String (31) "The server works
"
[1] = & gt;
String (31) "Server running"
}
[3] = & gt;
Array (1) {
[0] = & gt;
String (23) "Server Fell"
}
[4] = & gt;
Array (0) {
}
}