Jump to content

[SOLVED] Foreach argument problem


Sebby123

Recommended Posts

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

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'.";
    }
  }
}
?>

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.

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. =]

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.