我在这里做了一些快速的事情,实际上不是很漂亮,但它可能会有所帮助。
<?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);
这样的输出
<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"
}
}
在您的示例中,您使用的是$this->high = "true";,但您可能指的是$school->high = "true";。
它不是基于匹配拆分数据,但您可以循环正则表达式数组并将正则表达式作为参数传递给sortSchools($regex),对该参数执行 preg_match 并让函数返回匹配学校的数组.
这里没有愚蠢的问题;)。
除了preg_match,您还可以使用preg_grep 来获取数组中匹配学校的索引
preg_grep("/1[0-2]$/", explode("\n", $input_lines));。您可能会发现这个php live regex tester 很有用。
请注意,您也可以通过参考通过学校,请参阅PHP Documentation,但不建议这样做,请参阅this other question。