Jump to content

Array issues - Cannot use string offset as an array


Thatch

Recommended Posts

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

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.

 

 

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

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?

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

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.

Archived

This topic is now archived and is closed to further replies.

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