SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jun 11, 2001, 03:49 #1
- Join Date
- Feb 2001
- Location
- The Netherlands
- Posts
- 256
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Warning: Wrong datatype for first argument in call to in_array() in
Does anyone know why I get this error when I try to run the code below?
I checked it over and over and can't find anything wrong with it...I"m guessing the foreach statement causes this error, but to my understanding the code is correct.
Warning: Wrong datatype for first argument in call to in_array() in .... line 13
<?
// Define arrays
$domains = array ("strawberries.com","bananas.com","apples.com");
$selected = array ("strawberries.com","bananas.com");
foreach ($domains as $domainlist) {
if (in_array ($selected, $domainlist)) {
echo"<A HREF=$PHP_SELF?delete=$domainlist>Delete $domainlist from list</A><br>";
}
else {
echo"<A HREF=$PHP_SELF?add=$domainlist>Add $domainlist to list</A><br>";
}
}
?>
-
Jun 11, 2001, 04:52 #2
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Aha, and so we meet again Mr. Bond
Yeah, you are passing two arrays to in_array() when you want to be passing $needle (as a string or int or double, etc) and $haystack (as an array) like so:
in_array($needle, $haystack);
I did not know of in_array() until this moment, so I just looked it up in the manual. That's why I like hanging around here - you learn something new every day
-
Jun 11, 2001, 05:18 #3
- Join Date
- Feb 2001
- Location
- The Netherlands
- Posts
- 256
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Warning: Wrong datatype for first argument in call to in_array()
Thanks for the info freakysid,
Do you perhaps know of an efficient way of comparing 2 'haystacks', instead of comparing a 'needle' against a 'haystack' ?
By the way for the people who have "PHP Developer's Cookbook" , on page 85 they give an example on in_array :
$common = array ("Marriages","were","made","in","heaven");
$oscar_wilde = array ("Divorces","were","made","in","heaven");
$symdiff = array();
foreach ($common as $ele) {
if (!in_array ($oscar_wilde, $ele)) {
array_push ($symdiff, $ele);
}
}
...
.....
This gives the same error, since they use 2 arrays just like I did in my example.
-
Jun 11, 2001, 05:30 #4
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
LOL, they just have the arguements around the wrong way. I think they mean to write this as:
PHP Code:$common = array ("Marriages","were","made","in","heaven");
$oscar_wilde = array ("Divorces","were","made","in","heaven");
$symdiff = array();
foreach ($common as $ele) {
if (!in_array ($ele, $oscar_wilde)) {
array_push ($symdiff, $ele);
}
}
array_push ($symdiff, $ele);
Is a rather extravagent way of writing:
$sysmdiff[] = $ele;
But each to there own
-
Jun 11, 2001, 05:53 #5
- Join Date
- Feb 2001
- Location
- The Netherlands
- Posts
- 256
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
freakysid, thanks for clearing that up!
Bookmarks