PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 11:20 am
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
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed Apr 04, 2007 11:25 am
Is $res[0]['id'] an array or a string?
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 11:31 am
it's an array
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed Apr 04, 2007 11:34 am
Try declaring $cod as an array before assigning it.
Code: Select all
<?php
$cod = array();
$cod = $res[0]['id'];
?>
Does that throw errors?
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 11:43 am
yes, it throws the same error...
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed Apr 04, 2007 12:07 pm
Do a...
Code: Select all
<?php
echo '<pre>'; var_dump($res); echo '</pre>';
?>
And post back what he structure of the array is.
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 12:15 pm
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"
}
}
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed Apr 04, 2007 12:25 pm
$res[0]['id'] is a string, not an array. This should be a simple assignment.
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 12:28 pm
so, how can i solve my problem ?
can i use something like this to strings ?
$cod = $res[0]['id'];
thanks in advance
Last edited by
bouncer on Wed Apr 04, 2007 12:31 pm, edited 1 time in total.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed Apr 04, 2007 12:30 pm
Echo it. What does it echo? var_dump() it. What does that tell you? It should be a straight assignment.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed Apr 04, 2007 12:31 pm
You can. You've probably posted the wrong bit of code. Post your full script.
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Wed Apr 04, 2007 12:35 pm
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
)
thanks in advance
mentor
Forum Contributor
Posts: 100 Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan
Post
by mentor » Thu Apr 05, 2007 2:01 am
I dont know whether it will solve your problem or not, but atleast you can try something like this
OR
Code: Select all
$res[0]["id"] = (string) $res[0]["id"]
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Thu Apr 05, 2007 3:19 am
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
bouncer
Forum Contributor
Posts: 162 Joined: Wed Feb 28, 2007 10:31 am
Post
by bouncer » Thu Apr 05, 2007 3:48 am
(i have missed '')
if i use this,
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