在我看来,一个更容易使用的结构更像是这样的:
$matches = array(
array( 'name' => 'somename', 'priority' => $priority_level_for_this_match ),
array( 'name' => 'someothername', 'priority' => $priority_level_for_that_match )
)
要填充此数组,请先创建一个空数组:
$matches = array();
然后,找到所有匹配项。
$match = array( 'name' => 'somename', 'priority' => $some_priority );
要将该数组添加到您的匹配项中,只需将其放在末尾即可:
$matches[] = $match;
填充后,您可以轻松地对其进行迭代:
foreach($matches as $k => $v) {
// The value in this case is also an array, and can be indexed as such
echo( $v['name'] . ': ' . $v['priority'] . '<br>' );
}
您还可以根据优先级对匹配的数组进行排序:
function cmp($a, $b) {
if($a['priority'] == $b['priority'])
return 0;
return ($a['priority'] < $b['priority']) ? -1 : 1;
}
usort($matches, 'cmp');
(来自这个答案)