/    Sign up×
Community /Pin to ProfileBookmark

How to Check if Array Has An INT ?

Hi,

Without looping and checking each array value one by one, how to check if array got INT ?
I tried these. I get nothing echoed.

[code]
$array = array(‘string’,’1′,”11″,”111”,””1111″”,'”11111″‘,”‘111111′”);

if(!in_array(IS_INT($array),$array))
{
$intval = intval($array);
die($intval);
}
else
{
$intval = intval($array);
die($intval);
}
[/code]

[code]
$array = array(‘string’,’1′,”11″,”111”,””1111″”,'”11111″‘,”‘111111′”);

if(!in_array(IS_INT($array),$array))
{
$intval = intval($array);
die($intval);
}
else
{
$intval = intval($array);
die($intval);
}
[/code]

I know the difference between IS_INT() and IS_NUMERIC().

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 05.2022 — > @novice2022#1644518 I get nothing echoed

Because, in fact, you _have_ to loop through the array. Applying those functions to the array variable itself is not giving you any information about the elements of that array. Since an array is an array, not an integer, is_int($any_array) will always return false.
[code=text]
$ php -a
Interactive shell

php > $foo = 1;
php > $bar = '1';
php > $fubar = array(1, '1', 2, '3');
php > var_dump(is_int($foo));
bool(true)
php > var_dump(is_int($bar));
bool(false)
php > var_dump(is_int($fubar));
bool(false)
php >
[/code]
Copy linkTweet thisAlerts:
@Kroknamzpq2wztJun 06.2022 — You can use this var_dump(is_int($fubar)); it will return the int array values
Copy linkTweet thisAlerts:
@novice2022authorJun 06.2022 — @Kroknamzpq2wzt#1644546

Let's say I do not want to print_f or vardump but create a condition to do this if array has INT; else do that.

Then, how you would write the code in very simple terms ?
Copy linkTweet thisAlerts:
@NogDogJun 06.2022 — [code=php]
$test = ['one', '2', true, 3, 'foobar'];
if (has_int($test)) {
echo "It has an intn";
} else {
echo "It does NOT have an intn";
}

$test = ['one', '2', true, '3333', 'foobar'];
if (has_int($test)) {
echo "It has an intn";
} else {
echo "It does NOT have an intn";
}

function has_int(array $arr) {
foreach ($arr as $val) {
if (is_int($val)) {
return true;
}
}
return false;
}
[/code]

[code=text]
$ php ~/Desktop/has_int.php
It has an int
It does NOT have an int
[/code]
Copy linkTweet thisAlerts:
@Kroknamzpq2wztJun 07.2022 — I face the same situation for my two sites Fishing equipment and Web development I solve the issue using same methord

**Links removed by Site Staff so it doesn't look like you're spamming us. Please don't post them again.**
Copy linkTweet thisAlerts:
@NogDogJun 07.2022 — PS: Needed something to waste a few minutes, so for extra credit, here's the function made to work with multi-dimension arrays, and without explicit loops (though internally it's essentially still doing looping):
[code=php]
function has_int(array $arr) {
$found = false;
array_walk_recursive($arr, function($val, $key) use(&$found) {
if(is_int($val)) {
$found = true;
}
}
);
return $found;
}
[/code]
Copy linkTweet thisAlerts:
@tracknutJun 07.2022 — @NogDog#1644576 Good to know. I'll file this under "what will I do if they remove loops in PHP 12"" :)
Copy linkTweet thisAlerts:
@novice2022authorJun 08.2022 — @tracknut#1644577

WHat do you mean ?

Do you really like NogDog's code or were you being sarcastic to me or either him ?
Copy linkTweet thisAlerts:
@novice2022authorJun 08.2022 — @NogDog#1644576

I would not call that a waste of your or my or anybody's time.

Thanks for the code tip!
Copy linkTweet thisAlerts:
@tracknutJun 09.2022 — @novice2022#1644595 I might be missing some subtle and important use of array_walk_recursive(), but on cursory reading of the manual I found it only useful for the case of not wanting to write my own loop(s), while traversing an array. And conveniently, that's exactly what you asked NogDog for, so I found his answer perfect!
Copy linkTweet thisAlerts:
@NogDogJun 09.2022 — @tracknut#1644603

Main advantage here is that if you don't know how many dimensions the array has, it takes care of traversing them all for you. If you did it with foreach() loops, you'd have to stick them into a recursive function -- which is not a big deal once you've grokked how to do that. :)
×

Success!

Help @novice2022 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.6,
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,
)...