Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP进行过滤_Php_Filter - Fatal编程技术网

用PHP进行过滤

用PHP进行过滤,php,filter,Php,Filter,我在第1列有一个$data显示: “AAA123” “ABC1234” “ABD123” “BAC12” “CAB123” “DA125” 等等 我想显示以“AB”开头的$data,它在第1列显示我,如: “ABC1234” “ABD123” 不是其他行,而是与“ABC1234”和“ABD123”相关的其他行和列 提前准备好 使用strop(),如 if(strpos($my_string,“AB”)==0){ } 请务必使用==而不是==,因为如果找不到“AB”,则函数将返回false,使

我在第1列有一个$data显示:

“AAA123” “ABC1234” “ABD123” “BAC12” “CAB123” “DA125” 等等

我想显示以“AB”开头的$data,它在第1列显示我,如:

“ABC1234” “ABD123”

不是其他行,而是与“ABC1234”和“ABD123”相关的其他行和列


提前准备好

使用
strop
(),如

if(strpos($my_string,“AB”)==0){
}
请务必使用
==
而不是
==
,因为如果找不到“AB”,则函数将返回false,使用
=
使用
strpos
()将等于0,如

if(strpos($my_string,“AB”)==0){
}

请务必使用
==
而不是
=
,因为如果找不到“AB”,则函数将返回false,这将等于0,使用
=

我只需使用
substr()
,如下代码片段所示:

if (substr($str, 0, 2) == 'AB') {
  // The string is right.
}

我只需使用
substr()
,如下代码片段所示:

if (substr($str, 0, 2) == 'AB') {
  // The string is right.
}

如果
$data
是字符串数组,则可以使用

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);'
                  )               );
或:

或者您可以使用循环:

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;
    }
}
或:


如果
$data
是字符串数组,则可以使用

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);'
                  )               );
或:

或者您可以使用循环:

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;
    }
}
或:


$data
的类型是什么?字符串数组?
$data
是SQL查询的结果吗?不,我通过拆分从txt获取$data那么
$data
的类型是什么?字符串数组?数组的数组?如果是后者,内部数组的结构是什么?代码的屏幕截图远不如代码本身有用。您还应该选择缩进样式()并使用它。我个人推荐1TB.outis,也感谢其他人的关注。实际上,我不知道如何写这些东西,但只是根据需要编辑。我想我需要学习更多的编码而不是编辑。什么是
$data
类型?字符串数组?
$data
是SQL查询的结果吗?不,我通过拆分从txt获取$data那么
$data
的类型是什么?字符串数组?数组的数组?如果是后者,内部数组的结构是什么?代码的屏幕截图远不如代码本身有用。您还应该选择缩进样式()并使用它。我个人推荐1TB.outis,也感谢其他人的关注。实际上,我不知道如何写这些东西,但只是根据需要编辑。我认为我需要学习更多的编码,而不是编辑。我认为这是最接近的回答。现在我有了查询的确切行数——这意味着“strpos”正在工作——但整个表(行和列)变为空。知道吗?我想这是最接近的回答。现在我有了查询的确切行数——这意味着“strpos”正在工作——但整个表(行和列)变为空。有什么想法吗?