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'); Link to comment https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/ 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 Link to comment https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/#findComment-1119356 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. Link to comment https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/#findComment-1119389 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 Link to comment https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/#findComment-1119394 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 Link to comment https://forums.phpfreaks.com/topic/215226-operator-not-supported-for-strings/#findComment-1119411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.