Htm*_*sin 1 php explode array-filter
我以前在分解后对数组进行计数,使用可以解决,array_filter但是现在当我回显数组中的元素时,它给了我未定义的偏移量错误
$exclude=array();
$exclude[0]="with";
$exclude[1]="do";
$search="moving on with my car";
$sch1 = str_replace($exclude,"", trim($search));
$sch2 = explode(" ",trim($sch1));
$sch = array_filter($sch2);
// The value of the count is actually 4
// But when i try to display throws an indefined offset error
echo $sch[0];
echo $sch[1];
echo $sch[2]; // Throwing an "Undefined offset: 2" Error
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激 。谢谢
array_filter()删除等于的数组的所有条目FALSE,从而在数组中创建间隙。
这是$sch2包含:
Array
(
[0] => moving
[1] => on
[2] =>
[3] => my
[4] => car
)
Run Code Online (Sandbox Code Playgroud)
在上申请array_filter()时$sch2,您将获得一个类似于以下内容的数组:
Array
(
[0] => moving
[1] => on
[3] => my
[4] => car
)
Run Code Online (Sandbox Code Playgroud)
As you can see, index 2 is not defined. You need to re-index the array numerically for this to work. You can use array_values() for this purpose:
$sch = array_values(array_filter($sch2));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
825 次 |
| 最近记录: |