Sebby123 Posted July 31, 2009 Share Posted July 31, 2009 Hello there, I'm currently having problems with the foreach statement. I'm new to using foreach so I don't understand it very well. Could someone explain to me this problem and/or what is wrong with my code so that it produces this error? Warning: Invalid argument supplied for foreach() in /DirHere/AdditionCtrl.php on line 32 Lines 30 - 44: var_dump($Addon); /* array(2) { ["Hangman"]=> string(7) "Hangman" ["TicTakToe"]=> string(11) "Tic Tak Toe" } */ function GetAddonInfo($AddonName) { foreach ($Addon as $Folder => $Name) { if ($AddonName == $Name) { echo "Addon Information:\n"; echo "Name: ".GetAddonINI($Folder."/Info.ini", "Name"); echo "Description: ".GetAddonINI($Folder."/Info.ini", "Description"); echo "Author: ".GetAddonINI($Folder."/Info.ini", "Author"); echo "Email: ".GetAddonINI($Folder."/Info.ini", "Email"); echo "Website: ".GetAddonINI($Folder."/Info.ini", "Website"); } else { echo "Cannot find the addon '$AddonName'."; } } } Thank you in advanced. Seb. Link to comment https://forums.phpfreaks.com/topic/168295-solved-foreach-argument-problem/ Share on other sites More sharing options...
WolfRage Posted July 31, 2009 Share Posted July 31, 2009 In the foreach I think you meant that "$Addon" should be "$AddonName". <?php var_dump($Addon); /* array(2) { ["Hangman"]=> string(7) "Hangman" ["TicTakToe"]=> string(11) "Tic Tak Toe" } */ function GetAddonInfo($AddonName) { foreach ($AddonName as $Folder => $Name) { if ($AddonName == $Name) { echo "Addon Information:\n"; echo "Name: ".GetAddonINI($Folder."/Info.ini", "Name"); echo "Description: ".GetAddonINI($Folder."/Info.ini", "Description"); echo "Author: ".GetAddonINI($Folder."/Info.ini", "Author"); echo "Email: ".GetAddonINI($Folder."/Info.ini", "Email"); echo "Website: ".GetAddonINI($Folder."/Info.ini", "Website"); } else { echo "Cannot find the addon '$AddonName'."; } } } ?> Link to comment https://forums.phpfreaks.com/topic/168295-solved-foreach-argument-problem/#findComment-887694 Share on other sites More sharing options...
MatthewJ Posted July 31, 2009 Share Posted July 31, 2009 I would think something like: var_dump($Addon); /* array(2) { ["Hangman"]=> string(7) "Hangman" ["TicTakToe"]=> string(11) "Tic Tak Toe" } */ function GetAddonInfo($AddonName, $Addon) { foreach ($Addon as $Folder => $Name) { if ($AddonName == $Name) { echo "Addon Information:\n"; echo "Name: ".GetAddonINI($Folder."/Info.ini", "Name"); echo "Description: ".GetAddonINI($Folder."/Info.ini", "Description"); echo "Author: ".GetAddonINI($Folder."/Info.ini", "Author"); echo "Email: ".GetAddonINI($Folder."/Info.ini", "Email"); echo "Website: ".GetAddonINI($Folder."/Info.ini", "Website"); } else { echo "Cannot find the addon '$AddonName'."; } } } GetAddonInfo('addon name', $Addon); It looks like you were just trying to work on an array without passing it in to the function. Link to comment https://forums.phpfreaks.com/topic/168295-solved-foreach-argument-problem/#findComment-887703 Share on other sites More sharing options...
Sebby123 Posted July 31, 2009 Author Share Posted July 31, 2009 It looks like you were just trying to work on an array without passing it in to the function. Ah, yes, I presumed that it would have the variable $Addon because it was defined before the function. Didn't realise I had to give it to the function. Well now I know for next time as well. Thank you very much. =] Link to comment https://forums.phpfreaks.com/topic/168295-solved-foreach-argument-problem/#findComment-887761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.