Utherr12 Posted April 12, 2011 Share Posted April 12, 2011 Fatal error: Cannot use string offset as an arrayThe line that's being pointed at is $tags = "";foreach($json['plugin'] as $plugin) { //some code here foreach($plugin['tags']['tag'] as $tag) // this is the line with the fatal error $tags .= $tag." "; $tags = "";} i did var_dump for $plugin['tags']['tag'] and it's an array with two elements [0], [1]. I did this to the variable outside the functionIf i do var_dump on that array inside the function the same error occurs. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 12, 2011 Share Posted April 12, 2011 Can you show the output when you var_dump $json? Link to comment Share on other sites More sharing options...
Utherr12 Posted April 13, 2011 Author Share Posted April 13, 2011 array(1) { ["plugin"]=> array(15) { ["id"]=> string(7) "fortune" ["dokulink"]=> string(14) "plugin:fortune" ["name"]=> string(14) "fortune Plugin" ["description"]=> string(26) "Show QOTD (Fortune cookie)" ["author"]=> string(11) "Otto Vainio" ["type"]=> string(6) "Syntax" ["lastupdate"]=> string(10) "2005-08-01" ["compatible"]=> array(0) { } ["securityissue"]=> array(0) { } ["securitywarning"]=> array(0) { } ["tags"]=> array(1) { ["tag"]=> array(2) { [0]=> string(6) "quotes" [1]=> string(6) "random" } } ["downloadurl"]=> string(44) "http://koti.mbnet.fi/oiv/pubtest/fortune.zip" ["bugtracker"]=> array(0) { } ["donationurl"]=> array(0) { } ["relations"]=> array(3) { ["similar"]=> array(1) { ["id"]=> string(8) "xfortune" } ["conflicts"]=> array(0) { } ["depends"]=> array(0) { } } } } This error only happens when ["plugin"] is an array of 1, if it's 2+, the error doesn't occur. Link to comment Share on other sites More sharing options...
birbal Posted April 13, 2011 Share Posted April 13, 2011 , Link to comment Share on other sites More sharing options...
shadowayex Posted April 13, 2011 Share Posted April 13, 2011 Can you post the var_dump of $json in a case where the error would appear?Dump it before your loop. Link to comment Share on other sites More sharing options...
Utherr12 Posted April 15, 2011 Author Share Posted April 15, 2011 Can you post the var_dump of $json in a case where the error would appear?Dump it before your loop.It's the same as the above:array(1) { ["plugin"]=> array(15) { ["id"]=> string(7) "fortune" ["dokulink"]=> string(14) "plugin:fortune" ["name"]=> string(14) "fortune Plugin" ["description"]=> string(26) "Show QOTD (Fortune cookie)" ["author"]=> string(11) "Otto Vainio" ["type"]=> string(6) "Syntax" ["lastupdate"]=> string(10) "2005-08-01" ["compatible"]=> array(0) { } ["securityissue"]=> array(0) { } ["securitywarning"]=> array(0) { } ["tags"]=> array(1) { ["tag"]=> array(2) { [0]=> string(6) "quotes" [1]=> string(6) "random" } } ["downloadurl"]=> string(44) "http://koti.mbnet.fi/oiv/pubtest/fortune.zip" ["bugtracker"]=> array(0) { } ["donationurl"]=> array(0) { } ["relations"]=> array(3) { ["similar"]=> array(1) { ["id"]=> string(8) "xfortune" } ["conflicts"]=> array(0) { } ["depends"]=> array(0) { } } } } Rofl, if i dump_var($plugin['tags']) before the second iteration it shows string(1) "f"omfg, WHY Link to comment Share on other sites More sharing options...
Utherr12 Posted April 15, 2011 Author Share Posted April 15, 2011 I just found out why... because when only result is generated $plugin somehow is not an array, i just dumped $plugin and it gives me string(7) "fortune" (a result)So if I get multiple results my array looks like $json['plugin'][0]['id'] , if there was only one result it'd be like $json['plugin']['id'] Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2011 Share Posted April 15, 2011 That may be a problem with how the JSON string is being built. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.