Fatal error: Cannot use string offset as an array

前端 未结 2 1418
星月不相逢
星月不相逢 2021-02-06 17:54
  Array
(
    [0] => Array
        (
            [auth_id] => 1
            [auth_section] => Client Data Base
            [auth_parent_id] => 0
                     


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 18:05

    The problem is that the last entry in the array (2) does not have a sub array, but you're trying to access it anyway. You'll need to check if the entry exists and if it's an array before looping over it. Here an example using foreach:

    foreach ($array as $auth) {
        if (!empty($auth['sub']) && is_array($auth['sub'])) {
            foreach ($auth['sub'] as $sub) {
                if (!empty($sub['auth_id'])) {
                    echo $sub['auth_id'];
                }
            }
        }
    }
    

提交回复
热议问题