Omirion Posted October 5, 2010 Share Posted October 5, 2010 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'); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 5, 2010 Share Posted October 5, 2010 The error is very explicit. You can't use "[]" with strings, only with arrays. What are you attempting to do? Ken Quote Link to comment Share on other sites More sharing options...
Omirion Posted October 5, 2010 Author Share Posted October 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 5, 2010 Share Posted October 5, 2010 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 Quote Link to comment Share on other sites More sharing options...
Omirion Posted October 5, 2010 Author Share Posted October 5, 2010 Oh... i see... makes sense... Jesus I'm stupid... Thanks man Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.