/    Sign up×
Community /Pin to ProfileBookmark

Warning: in_array() [function.in-array]: Wrong datatype for second argument

Newbie question: I have a small snipet of code here

$filetype_img = array(“gif”, “jpg”, “jpeg”, “png”, “bmp”, “psd”, “psp”, “tif”);

if(in_array ($fileext, $filetype_img))
{
$filemime = “Image”;
$file_image = $icon_image;
}

The above code works great if it not in a function. Below throws the Warning: in_array() [function.in-array]: Wrong datatype for second argument

function getfileext($fileext){

if(in_array ($fileext, $filetype_img)){
$filemime = “Image”;
$file_image = $icon_image;
}

}

getfileext($ext); // $ext is a string value

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ratcatemeJan 18.2009 — try adding
[code=php]global $filetype_img[/code]
as the second line to your function or moving the line with the array of file types to inside your function

Scott,
Copy linkTweet thisAlerts:
@ThomasTuckerauthorJan 18.2009 — You are the man Scott!

I just put everything within the function and declared a few other vars global inside it. Works like a charm.

I had the concept of globals but didn't really grasp the applying them part. I'm learning, and with help from guys like you I stand a chance of actually being good enough to hurt myself.

Thanks a million.
Copy linkTweet thisAlerts:
@MindzaiJan 19.2009 — Just be aware that globals have their problems. For instance, if some other code accidentally changes the value of your global var you function no longer works. The point of functions (well, one of them) is to encapsulate functionality. Having them rely on external values is not good practice. You'd be better off either requiring the file_type array as an argument, or just defining it in the function itself where it cannot be altered by code outside the function.
Copy linkTweet thisAlerts:
@deludeddonnyFeb 17.2011 — Any idea why this is giving me "Warning: in_array() [function.in-array]: Wrong datatype for second argument "
<i>
</i>function isDayOK($ts) {
global $holidays;
global $daysWorking;
$dayDate = date(DATE_FORMAT, $ts);
$dayOfTheWeek = date('N', $ts);
if (in_array($dayDate, $holidays)) {
return false;
}
if (!$daysWorking[$dayOfTheWeek]) {
return false;
}
return true;
}
Copy linkTweet thisAlerts:
@svidgenFeb 17.2011 — Not without seeing the definition for $holidays ...
Copy linkTweet thisAlerts:
@NogDogFeb 17.2011 — In other words, for whatever reason, [b]$holidays[/b] is not an array -- which could possibly be because it is not defined in the global scope at all, but no way to be sure at this point. This is another good argument for not using [b]global[/b] and instead passing it as a function parameter (either by copy or by reference as needed). You might also want to code defensively then and check to see if it is an array before proceeding, either throwing an exception or returning false if it is not.
Copy linkTweet thisAlerts:
@deludeddonnyFeb 17.2011 — Here is the holiday array
[CODE]$holidays = array(
'01/01/2011',
'25/12/2011',
'20/03/2011'
);[/CODE]
Copy linkTweet thisAlerts:
@deludeddonnyFeb 17.2011 — It worked, I just removed all the global variables.
Copy linkTweet thisAlerts:
@WindwalkerJun 04.2011 — try adding
[code=php]global $filetype_img[/code]
as the second line to your function or moving the line with the array of file types to inside your function

Scott,[/QUOTE]


Let me please reactivate this thread.

I am currently working on integrating a Wordpress plugin.

I receive the above warning message, although I did not modify the code of the Wordpress plugin I just installed.

As the initial user in this thread said, the code, which throws the warning message, is placed inside a function and it uses an array defined outside the function.

I just followed Scotts advice and put the line
[code=php]global $<myarray>;[/code]
at the beginning of my function; but that doesn't help.

It only helps, if I define the array inside the function; but that causes need for further modifications, because the array is shared with other functions.

As a global variable, access to the array is easy, but now I need to alter the functions parameters, to pass the array (indeed its not only one array, but six different array) from one function to the next and vice versa.

Perhaps someone can describe the reason behind this warning and give some hint to get rid of that warning, without having to do too much modifications to the original Worpress plugin.

BTW, I am running php 5.2.6.

Thanks!
×

Success!

Help @ThomasTucker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...