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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.