Cannot use string offset as an array in php

前端 未结 10 821
南方客
南方客 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 07:04

    I believe what are you asking about is a variable interpolation in PHP.

    Let's do a simple fixture:

    $obj = (object) array('foo' => array('bar'), 'property' => 'value');
    $var = 'foo';
    

    Now we have an object, where:

    print_r($obj);
    

    Will give output:

    stdClass Object
        (
            [foo] => Array
                (
                    [0] => bar
                )
    
            [property] => value
        )
    

    And we have variable $var containing string "foo".

    If you'll try to use:

    $give_me_foo = $obj->$var[0];
    

    Instead of:

    $give_me_foo = $obj->foo[0];
    

    You get "Cannot use string offset as an array [...]" error message as a result, because what you are trying to do, is in fact sending message $var[0] to object $obj. And - as you can see from fixture - there is no content of $var[0] defined. Variable $var is a string and not an array.

    What you can do in this case is to use curly braces, which will assure that at first is called content of $var, and subsequently the rest of message-sent:

    $give_me_foo = $obj->{$var}[0];
    

    The result is "bar", as you would expect.

    0 讨论(0)
  • 2020-11-29 07:10

    I just want to explain my solving for the same problem.

    my code before(given same error):

    $arr2= ""; // this is the problem and solve by replace this $arr2 = array();
    for($i=2;$i<count($arrdata);$i++){
        $rowx = explode(" ",$arrdata[$i]);
        $arr1= ""; // and this is too
        for($x=0;$x<count($rowx);$x++){
            if($rowx[$x]!=""){
                $arr1[] = $rowx[$x];
            }
        }
        $arr2[] = $arr1;
    }
    for($i=0;$i<count($arr2);$i++){
        $td .="<tr>";
        for($j=0;$j<count($hcol)-1;$j++){
            $td .= "<td style='border-right:0px solid #000'>".$arr2[$i][$j]."</td>"; //and it's($arr2[$i][$j]) give an error: Cannot use string offset as an array
        }
        $td .="</tr>";
    }
    

    my code after and solved it:

    $arr2= array(); //change this from $arr2="";
    for($i=2;$i<count($arrdata);$i++){
        $rowx = explode(" ",$arrdata[$i]);
        $arr1=array(); //and this
        for($x=0;$x<count($rowx);$x++){
            if($rowx[$x]!=""){
                $arr1[] = $rowx[$x];
            }
        }
        $arr2[] = $arr1;
    }
    for($i=0;$i<count($arr2);$i++){
        $td .="<tr>";
        for($j=0;$j<count($hcol)-1;$j++){
            $td .= "<td style='border-right:0px solid #000'>".$arr2[$i][$j]."</td>";
        }
        $td .="</tr>";
    }
    

    Thank's. Hope it's helped, and sorry if my english mess like boy's room :D

    0 讨论(0)
  • 2020-11-29 07:11

    For PHP4

    ...this reproduced the error:

    $foo    = 'bar';
    $foo[0] = 'bar';
    

    For PHP5

    ...this reproduced the error:

    $foo = 'bar';
    
    if (is_array($foo['bar']))
        echo 'bar-array';
    if (is_array($foo['bar']['foo']))
        echo 'bar-foo-array';
    if (is_array($foo['bar']['foo']['bar']))
        echo 'bar-foo-bar-array';
    

    (From bugs.php.net actually)

    Edit,

    so why doesn't the error appear in the first if condition even though it is a string.

    Because PHP is a very forgiving programming language, I'd guess. I'll illustrate with code of what I think is going on:

    $foo = 'bar';
    // $foo is now equal to "bar"
    
    $foo['bar'] = 'foo';
    // $foo['bar'] doesn't exists - use first index instead (0)
    // $foo['bar'] is equal to using $foo[0]
    // $foo['bar'] points to a character so the string "foo" won't fit
    // $foo['bar'] will instead be set to the first index
    // of the string/array "foo", i.e 'f'
    
    echo $foo['bar'];
    // output will be "f"
    
    echo $foo;
    // output will be "far"
    
    echo $foo['bar']['bar'];
    // $foo['bar'][0] is equal calling to $foo['bar']['bar']
    // $foo['bar'] points to a character
    // characters can not be represented as an array,
    // so we cannot reach anything at position 0 of a character
    // --> fatal error
    
    0 讨论(0)
  • 2020-11-29 07:11

    I was able to reproduce this once I upgraded to PHP 7. It breaks when you try to force array elements into a string.

    $params = '';
    foreach ($foo) {
      $index = 0;
      $params[$index]['keyName'] = $name . '.' . $fileExt;
    }
    

    After changing:

    $params = '';
    

    to:

    $params = array();
    

    I stopped getting the error. I found the solution in this bug report thread. I hope this helps.

    0 讨论(0)
提交回复
热议问题