doni49 Posted December 20, 2015 Share Posted December 20, 2015 I'm trying to create a multi-dimensional array but it is throwing an error that just doesn't make sense. Does anyone see anything that could be causing this? This code: echo "<br />cnt: " . $cnt . "; k: " . $k . "; v: " . $v . "\n<br />"; echo "Type (cnt): " . gettype($cnt) . "<br />"; echo "Type (k): " . gettype($k) . "<br />"; echo "Type (v): " . gettype($v) . "<br />"; $values[$cnt][$k]= $v; Produces this: cnt: 1; k: date; v: 2015-12-10 Type (cnt): integer Type (k): string Type (v): string Fatal error: Cannot use string offset as an array in /home/........../putRecords.inc.php on line 47 Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/ Share on other sites More sharing options...
mac_gyver Posted December 20, 2015 Share Posted December 20, 2015 what is $values being created as? the error means it is being assigned a string value at some point. Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528290 Share on other sites More sharing options...
doni49 Posted December 20, 2015 Author Share Posted December 20, 2015 It is eventually used to insert values into a MySQL db using the following code: $query = "INSERT INTO `" . $tableName. "` (" . join("," , $fields) . ") VALUES ( " . join("," , $values) ." )"; Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528298 Share on other sites More sharing options...
mac_gyver Posted December 20, 2015 Share Posted December 20, 2015 no one asked what it is being used for. Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528299 Share on other sites More sharing options...
doni49 Posted December 20, 2015 Author Share Posted December 20, 2015 (edited) Sorry I really thought my reply answered that. But based on your statement, I went back and focused on the code other than line 47 and noticed that there was code further down in the for loop that reset the $values array as a string. I've since corrected that but now I'm getting a different error. The following code (I've confirmed there is no for loop involved here) produces the what's below that. echo "<pre>"; print_r($k); echo "</pre>"; echo "Value0: " . gettype($k[0]); echo "<br />Value1: " . gettype($k[1]); echo "<br />Type (k): " . gettype($k); $values = join(",",$k); Array ( [0] => ( 2015-12-10,"Jack McCoy",10.00,0,4,1 ) [1] => ( 2015-12-10,"Jack McCoy",10.00,0,4,1 ) ) Value0: string Value1: string Type (k): array Warning: join(): Invalid arguments passed in /home/........./putRecords.inc.php on line 78 Edited December 20, 2015 by doni49 Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528300 Share on other sites More sharing options...
mac_gyver Posted December 20, 2015 Share Posted December 20, 2015 (edited) your code and data doesn't produce that error for me. some of the possibilities would be - your actual code for the join(....) statement contains some characters that php doesn't recognize, but which appear normal after being put into the forum post (are you typing this code or copy/pasting it from somewhere where it could have been 'published' with some non-ascii character encoding?), the data itself or the point where you are displaying the debugging output contains or is in some html markup that's hiding what is really going on (what does the 'view source' in your browser show for that output?) or the code being posted or the line number being mentioned in the error doesn't actually correspond to what the code really is (this usually happens when you have multiple versions of code and the wrong one is being executed or you have multiple statements and the one producing the error is different from the one you are looking at/posting for us to look at.) you are basically asking someone to look at the tail ass end (symptom) of an animal and describe what the head (cause) looks like. this just results in a bunch of guessing and the problem could likely be a more obscure 4th or 5th thing that i didn't even mention. it's not that i/we don't trust you, it's that we have seen every possible combination of output and stated symptoms that don't tell the full story of what the code or result actually is. what is your full code from the point of the sql query statement up through where the current error is being reported at and are you outputting any html prior to this code that could be hiding some of the output? Edited December 20, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528303 Share on other sites More sharing options...
doni49 Posted December 21, 2015 Author Share Posted December 21, 2015 (edited) Mac, I have no idea what I changed that did it, but I seemed to have fixed it while trying to gather the info you requested. Very odd. Regarding the issue of trying to help with what info I was giving you, I can definitely understand what you're saying there -- on another forum that I frequent, I'm one of the guys that spends quite a bit of time helping others on the board and have dealt with quite a few issues where I felt like I was pulling teeth to get the info I needed to try and help them -- and I do greatly appreciate your effort. I was trying to avoid posting the entire code and asking you (or anyone else) to sort through it all to try to diagnose the issue. I really thought there must have been something with the code that posted -- just something obscure that I wasn't recognizing. EDIT: Regarding what I said about "what I changed", I don't REMEMBER changing anything other than adding a couple comments to help you understand what the code is doing. I really don't know what to make of that. Edited December 21, 2015 by doni49 Quote Link to comment https://forums.phpfreaks.com/topic/299896-help-with-error-msg-cannot-use-string-offset-as-an-array/#findComment-1528337 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.