Jump to content

[] operator not supported for strings


Omirion

Recommended Posts

This is the full error:

Fatal error: [] operator not supported for strings in D:\Programs\AppServ\www\test\test.php on line 19

 

I have comented on the error line.

 

 

function structure(){$argNum = func_get_args();$temp = $argNum;(int)$pointer = -1;$xmlStructure = array();foreach ($temp as $key => $value) {    if ($value == 'in') {            if ($pointer == -1) {                $pointer = 0;                    }                else{                $pointer = $pointer + 1;                    }        }elseif ($value == 'out') {            $pointer = $pointer - 1;          };    if($pointer != -1){        $xmlStructure[$pointer][] = $value; //THIS is the 19'th line.    }else{        $xmlStructure[] = $value;    }    }return $xmlStructure;    }function test_print($item2, $key){    echo "$key. $item2<br />\n";}$a = structure('0','in','00','01','02');array_walk($a,'test_print');

 

Link to comment
https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/
Share on other sites

As you can see $xmlStructure is defined as an array.

 

$xmlStructure = array();

 

Long story short.

 

structure(); Should return an array with all the arguments passed to it.

Say we have

$a = structure('one','two','pie');

 

$a should be $a[0] = one $a[1] = two $a[2] = pie

And it does, no problem there.

 

But if i pass an 'in' argument to it like so.

$b = structure('one','in','zeroOne','zeroTwo');

 

The output should be

$b[0] = one

$b[0][0] = zeroOne

$b[0][1] = zeroTwo

 

But instead it gives me that error.

I have no idea what the problem is.

The problem is occurring because the first time through, $xmlStructure[$pointer] is not an array, change that section of code to be:

<?php
    if($pointer != -1){
        if (!is_array($xmlStructure[$pointer])) {
           $xmlStructure[$pointer] = array();
        }
        $xmlStructure[$pointer][] = $value;
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.