I have the following string:
Apples Bananas Oranges Me 1 2 3 Brother 4 4 5
I need a regular expression to get number of apples, bananas and oranges each of us "me and my brother" has.
please help me, I'm totally clueless here.
I have the following string:
Apples Bananas Oranges Me 1 2 3 Brother 4 4 5
I need a regular expression to get number of apples, bananas and oranges each of us "me and my brother" has.
please help me, I'm totally clueless here.
You probably want some code like this:
$string = file('input.txt');
$headers = array();
$results = array();
foreach($string as $i => $line) {
preg_match_all('@(\w+)@', $line, $matches);
if (!$i) {
$headers = $matches[0];
} else {
$name = array_shift($matches[0]);
$results[$name] = array_combine($headers, $matches[0]);
}
}
print_r($results);
which would result in:
Array
(
[Me] => Array
(
[Apples] => 1
[Bananas] => 2
[Oranges] => 3
)
[Brother] => Array
(
[Apples] => 4
[Bananas] => 4
[Oranges] => 5
)
)