Если $data
- это массив строк, вы можете использовать array_filter .
PHP 5.3 или более поздняя версия:
$AB = array_filter($data, function($str) {
return 'AB' == substr($str, 0, 2);
});
до PHP 5.3:
$AB = array_filter($data,
create_function('$str',
'return "AB" == substr($str, 0, 2);'
) );
Или:
function create_prefix_tester($prefix) {
return create_function('$str',
"return '$prefix' == substr(\$str, 0, " . strlen($prefix) . ');'
);
}
$AB = array_filter($data, create_prefix_tester('AB'));
Или вы можете использовать цикл:
foreach ($data as $str) {
if ('AB' == substr($str, 0, 2)) {
// do stuff
...
}
}
Редактировать
Из примера кода, похоже, вы захотите цикл:
while (FALSE !== ($line = fgets($fp))) {
$row = explode('|', $line); // split() is deprecated
if ('AB' == substr($row[0], 0, 2)) {
switch($sortby) {
case 'schools': // fallthru
default:
$sortValue = $row[0];
break;
case 'dates':
$sortValue = $row[1];
break;
case 'times':
$sortValue = $row[2];
break;
}
array_unshift($row, $sortValue);
$table[] = $row;
}
}
или
function cmp_schools($a, $b) {
return strcmp($a[0], $b[0]);
}
function cmp_dates($a, $b) {
return $a['datestamp'] - $b['datestamp'];
}
function cmp_times($a, $b) {
return $a['timestamp'] - $b['timestamp'];
}
while (FALSE !== ($line = fgets($fp))) {
$row = explode('|', $line); // split() is deprecated
if ('AB' == substr($row[0], 0, 2)) {
$when = strtotime($row[1] + ' ' + $row[2]);
$row['timestamp'] = $when % (60*60*24);
$row['datestamp'] = $when - $row['timestamp'];
$table[] = $row;
}
}
usort($table, 'cmp_' + $sortby);