I did something quick here, that's actually not very beautiful, but it may help.
<?php
class School {
public $gradeRange;
public $name;
public $address;
public $city;
public $high;
public function __construct($gradeRange, $name, $address, $city) {
$this->gradeRange = $gradeRange;
$this->name = $name;
$this->adress = $address;
$this->city = $city;
}
}
$data = array(
new School(112, "springfield hs", "99 strt", "Springfield"),
new School(111, "stuff hs", "blvd stuff", "stufftown"),
new School(134, "univerisity (not an highschool)", "here", "charlestown"),
new School(110, "paul", "23 rd", "poll"),
);
foreach ($data as $school) {
if (preg_match("/1[0-2]$/", $school->gradeRange)) { ?>
<tr>
<td><?= $school->name ?></td>
<td>
<address><?= $school->address ?>, <?= $school->city ?></address>
</td>
</tr>
<?php }
}
class SchoolSorter
{
public $schools;
/**
* @param array|School $schools
*/
public function __construct(array $schools)
{
//$xmlReadre = new XMLReader();
//$xmlReadre->readString($xml);
$this->schools = $schools;
}
public function sortSchools()
{
foreach ($this->schools as $school) {
if (preg_match("/1[0-2]$/", $school->gradeRange)) {
$school->high = "true";
}
}
}
}
$that_scholl_sorter = new SchoolSorter(
$data
);
echo "before: \n";
var_dump($that_scholl_sorter->schools);
$that_scholl_sorter->sortSchools();
echo "after: \n";
var_dump($that_scholl_sorter->schools);
which outputs like this
<tr>
<td>springfield hs</td>
<td>
<address>, Springfield</address>
</td>
</tr>
<tr>
<td>stuff hs</td>
<td>
<address>, stufftown</address>
</td>
</tr>
<tr>
<td>paul</td>
<td>
<address>, poll</address>
</td>
</tr>
before:
array(4) {
[0]=>
object(School)#1 (6) {
["gradeRange"]=>
int(112)
["name"]=>
string(14) "springfield hs"
["address"]=>
NULL
["city"]=>
string(11) "Springfield"
["high"]=>
NULL
["adress"]=>
string(7) "99 strt"
}
[1]=>
object(School)#2 (6) {
["gradeRange"]=>
int(111)
["name"]=>
string(8) "stuff hs"
["address"]=>
NULL
["city"]=>
string(9) "stufftown"
["high"]=>
NULL
["adress"]=>
string(10) "blvd stuff"
}
[2]=>
object(School)#3 (6) {
["gradeRange"]=>
int(134)
["name"]=>
string(31) "univerisity (not an highschool)"
["address"]=>
NULL
["city"]=>
string(11) "charlestown"
["high"]=>
NULL
["adress"]=>
string(4) "here"
}
[3]=>
object(School)#4 (6) {
["gradeRange"]=>
int(110)
["name"]=>
string(4) "paul"
["address"]=>
NULL
["city"]=>
string(4) "poll"
["high"]=>
NULL
["adress"]=>
string(5) "23 rd"
}
}
after:
array(4) {
[0]=>
object(School)#1 (6) {
["gradeRange"]=>
int(112)
["name"]=>
string(14) "springfield hs"
["address"]=>
NULL
["city"]=>
string(11) "Springfield"
["high"]=>
string(4) "true"
["adress"]=>
string(7) "99 strt"
}
[1]=>
object(School)#2 (6) {
["gradeRange"]=>
int(111)
["name"]=>
string(8) "stuff hs"
["address"]=>
NULL
["city"]=>
string(9) "stufftown"
["high"]=>
string(4) "true"
["adress"]=>
string(10) "blvd stuff"
}
[2]=>
object(School)#3 (6) {
["gradeRange"]=>
int(134)
["name"]=>
string(31) "univerisity (not an highschool)"
["address"]=>
NULL
["city"]=>
string(11) "charlestown"
["high"]=>
NULL
["adress"]=>
string(4) "here"
}
[3]=>
object(School)#4 (6) {
["gradeRange"]=>
int(110)
["name"]=>
string(4) "paul"
["address"]=>
NULL
["city"]=>
string(4) "poll"
["high"]=>
string(4) "true"
["adress"]=>
string(5) "23 rd"
}
}
In your example, you were using $this->high = "true";
, but you probably meant $school->high = "true";
.
It's not splitting data based on the match, but you could loop on an array of regular expressions and pass the regex as a parameter to sortSchools($regex)
, do the preg_match on that parameter and have the function return an array of matching schools.
No dumb question here ;).
Instead of preg_match
, you could also use preg_grep
to get the indexes of the matching schools in an array
preg_grep("/1[0-2]$/", explode("\n", $input_lines));
. You may find this php live regex tester useful.
Note that you could also pass schools by reference, see PHP Documentation, but that is not recommended, see this other question.