Cannot use string offset as an array in php

前端 未结 10 850
南方客
南方客 2020-11-29 06:35

I\'m trying to simulate this error with a sample php code but haven\'t been successful. Any help would be great.

\"Cannot use string offset as an array\"

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 06:55

    The error occurs when:

    $a = array(); 
    $a['text1'] = array();
    $a['text1']['text2'] = 'sometext';
    

    Then

    echo $a['text1']['text2'];     //Error!!
    

    Solution

    $b = $a['text1'];
    echo $b['text2'];    // prints: sometext
    

    ..

提交回复
热议问题