Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to keep only none falsy numbers in an array with PHP

1 Answer

0 votes
$array = [5, 6, null, "javascript", "", null, 9, null, null, 0, null, 8, 3.14, 85];

// Use array_filter to filter out non-numeric or "falsy" values
$array = array_filter($array, function($value) {
    return is_numeric($value) && $value != 0;
});

print_r(array_values($array)); // Re-index the array to reset keys



/*
run:

Array
(
    [0] => 5
    [1] => 6
    [2] => 9
    [3] => 8
    [4] => 3.14
    [5] => 85
)

*/

 



answered Mar 19, 2025 by avibootz
...