array type issue

They have: 461 posts

Joined: Jul 2003

the error (returns added at "foreach() in" to prevent horizontal scrolling):

<?php
Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294

Warning
: Invalid argument supplied for foreach()
in /home/joshua/includes/fyd.unifunc.php on line 294
?>
the function (adjusted to try to prevent horizontal scrolling):
<?php
function array_to_options($array, $selected){
 
$optlist=''; # make an empty string
 
foreach($array as $key=>$value){
    if(
$selected==$key){
# if this is the first time it wont match, so we don't need to care if it's set
     
$optlist=$optlist.\"<option value=\\"$key\\" selected>$value</option>\";
    }else{
     
$optlist=$optlist.\"<option value=\\"$key\\">$value</option>\";
    }
  }
  return
$optlist;
}
?>
some example arrays that are used:
<?php
# eye color
$eyecolor=array('0'=>'Blue', '1'=>'Brown', '2'=>'Green', '3'=>'Grey', '4'=>'Hazel');
# hair color
$haircolor=array('0'=>'Black', '1'=>'Blonde', '2'=>'Brown', '3'=>'Dyed', '4'=>'Red');
?>

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

They have: 93 posts

Joined: Jun 2003

Not seeing much wrong, but maybe your passing is not coming through properly.

Perhaps echo the array out to test it is an array, or use the is_array() function to test it.

I would suggest pastebin.com so we can see the whole script file with line numbers.

[Design Alpha] -Web Services : Design,Hosting,Advertising,Software
Ask about custom pricing on hosting!!
Site Assets: [UltraGaming.com] [Blades of Warcraft]

They have: 461 posts

Joined: Jul 2003

line numbers are moot because the file is huge and i'm merely giving you the function where the error occurs. the exact line is the that starts the foreach call.

i showed you two of the arrays. ALL the arrays used are sett up the same way.

edit: shane, have you looked at this???

http://us4.php.net/manual/en/control-structures.foreach.php

Quote: PHP 4 (not PHP 3) includes a foreach construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variables. There are two syntaxes; the second is a minor but useful extension of the first:

foreach(array_expression as $value) statement
foreach(array_expression as $key => $value) statement

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Have you checked that the function is getting the array information?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I see nothing wrong with your function.

The only cause for the error you're getting (that I've seen), is that the variable passed to the foreach() call is not an array.

<?php
function array_to_options($array, $selected){
 
$optlist=''; # make an empty string
print_r($array);exit;
  foreach(
$array as $key=>$value){
    if(
$selected==$key){
# if this is the first time it wont match, so we don't need to care if it's set
     
$optlist=$optlist.\"<option value=\\"$key\\" selected>$value</option>\";
    }else{
     
$optlist=$optlist.\"<option value=\\"$key\\">$value</option>\";
    }
  }
  return
$optlist;
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 461 posts

Joined: Jul 2003

well. i know it works because i was calling it from a test file to test it initially. this is the testfile:

<?php
include(\"/home/joshua/includes/fyd.incs.php\"); # includes file
$month=$_POST['month'];
if(!isset(
$month)){
  echo <<<END
    <html><head></head><body>
    <form action=\"
$_SERVER[PHP_SELF]\" method=\"POST\">
    <input type=\"text\" name=\"month\">
    <input type=\"submit\" value=\"check\">
    </form>
    </body></html>
END;
}else{
 
$array=array('00'=>'Month', '01'=>'January', '02'=>'February',
'03'=>'March', '04'=>'April', '05'=>'May',
'06'=>'June', '07'=>'July', '08'=>'August',
'09'=>'September', '10'=>'October', '11'=>'November',
'12'=>'December');
 
$selected=$_POST['month'];
 
$list=array_to_options($array, $_POST['month']);
  echo '<select name=\"test\" size=\"1\">'.
$list.'</select>';
}
?>
note: the testfile NEVER genereated an error of ANY kind

NOTE 2: THE ARRAYS ARE DEFINED BELOW THE FUNCTION IN THE SAME FILE, THUS IF THE FUNCTION IS BEING INCLUDED, SO ARE THEY, AND TWO OF THEM ARE IN THE INITAL POST. THE CALLS IN THE TWO FILES THAT USE THEM ARE LIKE THIS: $eyecoloroptlist=array_to_options($eyecolor, $_POST['eye']);

note 3: edited to stop horizontal scroll

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

They have: 461 posts

Joined: Jul 2003

update: figuring someone was going to ask how the test file handles the $months array internal to the include file i decided to modify and try it. works fine.

i was begining to think it was an issue with the array in the function call. hat i needed to call it $array[] but that's obviously not the case, so what the hell is goingon here?

edit:
update:

the pages are called by functions so that i can have an if/elseif block to find which is approriate. as soon as i added an include lin in the function it worked.
obviously the problem was where they were being declared

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.