【问题标题】:Accessing array returned from str_split() throws me undefined offset error访问从 str_split() 返回的数组会引发未定义的偏移错误
【发布时间】:2015-11-16 07:11:11
【问题描述】:

我需要将字符串12345 拆分为一个字符数组。使用我得到的以下代码:

未定义的偏移误差

否则,我使用print_r() 函数回显了完整的数组。

$arr = str_split('12345');
echo $arr[1];

编辑:

完整代码:

$id  = 4;
$sql = "SELECT * FROM `time_table` WHERE user_info_iduser_info = $id OR tutor_detail_idtutor_detail = $id";
$result = mysqli_query($conn,$sql);// 5 rows selected

 while($row = mysqli_fetch_row($result)){
     echo $row[3];  // outputs 12345
     $arr = str_split($row[3]);
     echo $arr[1]; // should output 2 but not working :(
}

【问题讨论】:

标签: php arrays mysqli split


【解决方案1】:

使用下面的代码代替str_split

$str="12345";
$str_len = strlen( $str );
$arr = array();
for ( $i = 0; $i < $str_len; $i++ )
{
    $arr[] = $str[$i];
    echo "<br>".$str[$i]."<br>";
}
print_r( $arr );

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2015-11-24
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多