problem with arrays

1

I would like to know why this happens:

$ c [0] = 'a'; $ c [0] [0] = 'b';

so far so good, but if I add

$ c [0] [0] [0] = 'c';

gives the error Fatal error: Can not use string offset as an array in H: \ aNoti \ web \ work \ miniforo \ pb.php on line 3

    
asked by ruben 04.11.2017 в 14:43
source

1 answer

0

Several things:

  • Your first assignment: $c[0]='a'; creates an array, and adds the value a in the first position.

    Let's try:

    $c[0]='a';
    var_dump($c);
    

    Result:

    array(1) {
      [0]=>
      string(1) "a"
    }
    
  • Instead, your second assignment replaces that value. Let's see:

    $c[0]='a';
    $c[0][0]='b';
    var_dump($c);
    

    Result:

    array(1) {
      [0]=>
      string(1) "b"
    }
    

    As you can see, you did not add a second value to your array, but you replaced the value of a previously entered.

  • By doing this: $c[0][0][0]='c'; you have the error :

  •   

    Uncaught Error: Can not use string offset as an array

    What if we see what is really $c[0][0][0] ? Let's see:

        $c[0]='a';
        $c[0][0]='b';
        var_dump($c[0][0][0]);
    
    Resultado:
    
        string(1) "b"
    

    Then what the error tells you is that $c[0][0][0] is a string, whose value is b and you can not use it as an array, which is what you try ... enter c in a certain position of the array.

    Several solutions:

    One-dimensional array

        $c[0]='a';
        $c[1]='b';
        $c[2]='c';  
        print_r($c);
    

    Result:

        Array
        (
            [0] => a
            [1] => b
            [2] => c
        )
    

    Note: The construction can also be done in this style:

        $c=array('a','b','c');
    

    Two-dimensional array

        $c[0][0]='a';
        $c[0][1]='b';
        $c[0][2]='c';  
        print_r($c);
    

    Result:

        Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )
    
        )
    

    Three-dimensional array

    With each value in a separate array:

    $c[0][0][0]='a';
    $c[1][0][0]='b';
    $c[2][0][0]='c';  
    print_r($c);
    

    Result:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => a
                    )
    
            )
    
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => b
                    )
    
            )
    
        [2] => Array
            (
                [0] => Array
                    (
                        [0] => c
                    )
    
            )
    
    )
    

    With the three values in the same array:

        $c[0][0][0]='a';
        $c[0][0][1]='b';
        $c[0][0][2]='c';  
        print_r($c);
    

    Result:

        Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => a
                            [1] => b
                            [2] => c
                        )
    
                )
    
        )
    
        
    answered by 04.11.2017 в 16:38