Page 1 of 5

PHP Fatal error

Posted: Wed Apr 04, 2007 11:20 am
by bouncer
Hi,

i have some problems with this fatal errors since i update my php version to 5.0.4, and i dont know why this happens:

Code: Select all

$cod = $res[0]['id'];

PHP Fatal error:  Cannot use string offset as an array
this fatal error appears when i use arrays like this $array[0]['something'] and if i use them inside a if statement, if i remove [0] the error doesnt appear anymore, but my page stopps working.

anyone knows whats hapening ?

thanks in advance

Posted: Wed Apr 04, 2007 11:25 am
by RobertGonzalez
Is $res[0]['id'] an array or a string?

Posted: Wed Apr 04, 2007 11:31 am
by bouncer
it's an array

Posted: Wed Apr 04, 2007 11:34 am
by RobertGonzalez
Try declaring $cod as an array before assigning it.

Code: Select all

<?php
$cod = array();
$cod = $res[0]['id'];
?>
Does that throw errors?

Posted: Wed Apr 04, 2007 11:43 am
by bouncer
yes, it throws the same error... :oops:

Posted: Wed Apr 04, 2007 12:07 pm
by RobertGonzalez
Do a...

Code: Select all

<?php
echo '<pre>'; var_dump($res); echo '</pre>';
?>
And post back what he structure of the array is.

Posted: Wed Apr 04, 2007 12:15 pm
by bouncer

Code: Select all

array(1) {
  [0]=>
  array(8) {
    ["id"]=>
    string(4) "1272"
    ["data"]=>
    string(10) "2007-04-04"
    ["produtoid"]=>
    string(4) "3069"
    ["codigo"]=>
    string(6) "710089"
    ["preco"]=>
    string(5) "79.90"
    ["descricao"]=>
    string(0) ""
    ["auxiliar"]=>
    string(1) "0"
    ["site"]=>
    string(1) "1"
  }
}

Posted: Wed Apr 04, 2007 12:25 pm
by RobertGonzalez
$res[0]['id'] is a string, not an array. This should be a simple assignment.

Posted: Wed Apr 04, 2007 12:28 pm
by bouncer
so, how can i solve my problem ?

can i use something like this to strings ?

$cod = $res[0]['id'];

thanks in advance

Posted: Wed Apr 04, 2007 12:30 pm
by RobertGonzalez
Echo it. What does it echo? var_dump() it. What does that tell you? It should be a straight assignment.

Posted: Wed Apr 04, 2007 12:31 pm
by Chris Corbyn
You can. You've probably posted the wrong bit of code. Post your full script.

Posted: Wed Apr 04, 2007 12:35 pm
by bouncer
i'm getting problems in $res[0]['id']....

Code: Select all

if(strlen(trim($res[0]['id'])) > 0){
	$pro = $res[0]['ptd'];
	$pre = $res[0]['desc'];
}
if i echo it i get 3026 , and with var_dump() i get string(4) "3026"

what i have to change in $res[0]['id'] ?? (i'm confused :oops: )

thanks in advance

Posted: Thu Apr 05, 2007 2:01 am
by mentor
I dont know whether it will solve your problem or not, but atleast you can try something like this

Code: Select all

settype($res[0]["id'], "string")
OR

Code: Select all

$res[0]["id"] = (string) $res[0]["id"]

Posted: Thu Apr 05, 2007 3:19 am
by bouncer
thanks mentor for your tip, but the fatal error appears anyway

inside the $res array i have strings and i want to select a specific string, as you can see above, before the update to php(5.0.4) i dont get this kind of error, now i get this error in every part that i want to select a specific string inside an array

thanks in advance

Posted: Thu Apr 05, 2007 3:48 am
by bouncer
(i have missed '') :lol:

if i use this,

Code: Select all

settype($res[0]['id'], "string")
fatal error disappear :) , so i have to use this function every time that i want to select a string form an array ?

if i want to select more than onde string in an array i have to call this function to every one of them ?

thanks in advance