Jump to content

Cannot use string offset as an array


Utherr12

Recommended Posts

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

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

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

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

Archived

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

×
×
  • Create New...