Thatch Posted April 9, 2010 Share Posted April 9, 2010 I've hit a wall with this one and just can't figure out what I'm doing wrong. I've pulled data like this a number of times so I'm not sure what is different this time other than the size of the string.... Any help with this would be greatly appreciated. I've spent way too much time on this. Here is the basic cod that is being used... $query=$this->db->get('summary'); //return $query->result_array(); foreach ($query->result_array() as $row) { if (!Empty($row['S001C'] )) { print_r($row); $name = $row['S001']; $comment[]= $row['S001C']; print_r( $comment); $data['result'][$name] = $comment; The last line is where things blow up. Here is what the variables equal as reported by the print_r() $query = Array ( [id] => 11 [s001] => Rocky S2V [s001C] => I did not evaluate these boots, but I own a pair. The Rocky S2V is the most comfortable boot I have ever worn in uniform. I have conducted CQB, Airborne Ops, FRIES, regular patrolling in the woods....These boots have stood up to everything I've done in them. I would recommend these boots to any one and was happy to see that they were one of the boots selected for the test. [s002_a] => No [s002_b] => No [s002_c] => No [s002_d] => No [s002_e] => No [s002C] => ) $comment = Array ( [0] => I did not evaluate these boots, but I own a pair. The Rocky S2V is the most comfortable boot I have ever worn in uniform. I have conducted CQB, Airborne Ops, FRIES, regular patrolling in the woods....These boots have stood up to everything I've done in them. I would recommend these boots to any one and was happy to see that they were one of the boots selected for the test. ) I tried making comment a string variable, forcing it into an array, and accessing and assigning it directly $data['result'][$name] = $row['S001C'] and nothing would take. I need it in this form to send it into my table building function. I would greatly appreciate a bit of guidance on this one. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/ Share on other sites More sharing options...
Thatch Posted April 9, 2010 Author Share Posted April 9, 2010 Just to clarify .... 'when things blow up' equates to the following error Fatal error: Cannot use string offset as an array in ....C:\...results_model.php on line 370 I put the information into the title but thought it might not be completely clear. Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039282 Share on other sites More sharing options...
andrewgauger Posted April 9, 2010 Share Posted April 9, 2010 I think a space is a special character and as such can't be used as a key in an array, try taking comment out and assigning just "j" to the $data['result'][$name] = "j" to rule that out. Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039284 Share on other sites More sharing options...
mrMarcus Posted April 9, 2010 Share Posted April 9, 2010 $data and $comment are both arrays; $data['result'][$name] = $comment[0]; would work. or you can merge the arrays with array_merge() EDIT: where is $data being set, anyways? basically, the error is trying to tell you that $data has already been set/cast as a string, and you are now incorrectly trying to populate it. Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039291 Share on other sites More sharing options...
Thatch Posted April 9, 2010 Author Share Posted April 9, 2010 Thanks for both of the replies. Actually I had ruled out both of those previously but to make sure I went back and retried what you suggested (in case I had changed something else I had forgotten about and I still got the same thing. I changed both the key and the value to a single alpha character (though I have other arrays that I am using that have Keys with spaces) and got the same error. On the array suggestion, the reason I declared $comment an array was that I had this problem when it was a simple sting and this was an attempt to fix it. To make sure I just retried your suggestion. Same thing as well. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039293 Share on other sites More sharing options...
mrMarcus Posted April 9, 2010 Share Posted April 9, 2010 check my edit Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039294 Share on other sites More sharing options...
andrewgauger Posted April 9, 2010 Share Posted April 9, 2010 $comment an array was that I had this problem when it was a simple sting and this was an attempt to fix it. Tell me about this, that actually might be an easier fix. Rather than defining comment as an array, why not use it as a string as it should be and fix the other error. Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039299 Share on other sites More sharing options...
Thatch Posted April 9, 2010 Author Share Posted April 9, 2010 Thanks MrM. I do believe that was it. I am using the $data array to handle data passing in and out of various functions. It seems that it was holding it's declaration from previous and bucking anything that I tried to put into it . (including strings) I simply did an unset($data) as I entered the function and it seems to have cleared the problem. Well there's 3 hours of my life I'll not be getting back. Thanks for your help. *** just saw your post Andrew, I'll be going back to $comment as a string at this point, well actually I'll probably assign it directly from the result_array if it lets me. Thanks again... I was out of ideas. Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039302 Share on other sites More sharing options...
andrewgauger Posted April 9, 2010 Share Posted April 9, 2010 http://php.net/manual/en/function.unset.php This might help if you don't want to have to go back and change a lot of code unset(); Link to comment https://forums.phpfreaks.com/topic/198071-array-issues-cannot-use-string-offset-as-an-array/#findComment-1039304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.