Filtering with array functions [part 3]

About the series

array_filter()

function odd($var) 
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
Odd :
Array (
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array (
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)

array_reduce()

function addComma($e1,$e2) 
{
return $e1 . ", " . $e2;
}
$dwarfs = array('Doc', 'Grumpy', 'Happy', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy');
print_r(array_reduce($dwarfs,"addComma"));
// Doc, Grumpy, Happy, Sleepy, Dopey, Bashful, Sneezy
print_r(array_reduce($dwarfs,"addComma", "Dwarfs"));
// Dwarfs, Doc, Grumpy, Happy, Sleepy, Dopey, Bashful, Sneezy

array_intersec()

$dwarfs1 = array('Doc', 'Grumpy', 'Happy', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy'); 
$dwarfs2 = array('Doc', 'Dopey');
$dwarfs3 = array('Doc', 'Grumpy', 'Dopey', 'Bashful', 'Sneezy');
$result=array_intersect($dwarfs1, $dwarfs2, $dwarfs3);
print_r($result);
Array (
[0] => 'Doc',
[1] => 'Dopey'
)

array_intersect_assoc()

$dwarfs = array( 
'first' => 'Grumpy',
'second' => 'Happy',
'third' => 'Sleepy',
'fourth' => 'Dopey',
'fifth' => 'Bashful',
'sixth' => 'Sneezy',
'seventh' => 'Doc'
);
$dwarfs2 = array(
'first' => 'Grumpy',
0 => 'Happy',
1 => 'Dopey',
2 => 'Bashful',
'sixth' => 'Sneezy',
);
$dwarfs3 = array(
'first' => 'Grumpy',
0 => 'Dopey',
'sixth' => 'Sneezy',
);
$result=array_intersect_assoc($dwarfs1, $dwarfs2, $dwarfs3);
print_r($result);
Array (
['first' ] => 'Grumpy',
['sixth'] => 'Sneezy'
)

array_diff_assoc()

function comparison($a,$b) 
{
Return $a ⇔ $b;
}
$dwarfs1 = array('Doc', 'Grumpy', 'Sneezy');
$dwarfs2 = array('Doc', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy');
print_r(array_uintersect($dwarfs1, $dwarfs2, "comparison")); Array (
[0] => 'Doc',
[1] => 'Sneezy'
)
  • array_intersect,
  • array_ intersect_ assoc,
  • array_ intersect_ key,
  • array_ intersect_ uassoc,
  • array_ intersect_ ukey,
  • array_ diff
  • array_ diff_ assoc,
  • array_ diff_ key,
  • array_ diff_ uassoc,
  • array_ diff_ ukey,

max()

$maxNumber = max(2, 3, 1, 6, 7); 
// 7
$maxArrayElement = max([2, 4, 5]);
// 5
$maxDwarfs = ['Doc', 'Grumpy', 'Happy', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy'];
// 'Doc'
// The string 'hello' when compared to an int is treated as 0
$maxIntString = max(1, 'Doc');
// 1
// Multiple arrays compares the elements from left to right 3 < 4
$maxArray = max([1, 2, 3], [1, 2, 4]);
// [1, 2, 4]
// The value 0 is evaluated as false whereas TRUE is evaluates as 1
$maxIntBool = max(0, TRUE);
// TRUE

min()

count() & sizeof()

$dwarfs = ['Doc', 'Grumpy', 'Happy', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy']; 
var_dump(count($dwarfs));
// int(7)
$disney = [
"dwarfs" =>
['Doc', 'Grumpy', 'Happy', 'Sleepy', 'Dopey', 'Bashful', 'Sneezy'],
"originals" =>
["Mickey Mouse","Pete", "Goofy", "Minnie Mouse", "Pluto"] ];
// Normal count
var_dump(count($disney));
// int(2)
// Recursive count
var_dump(count($disney));
// int(14)

Conclusion

Learn to code, gain a new skill, get a new job

Whatever your goal — Treehouse will get you there

--

--

--

An Italian coder and design enthusiast. I publish content on my blog that helps web developers improve their skills and share knowledge. http://anastasionico.uk

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Your supply chain data is already in there, let's unlock it, and make smarter decisions!

The Path to a Successful DevOps Career: (Part 4)

Notes on Running GUI Applications in Docker

A 120 Days Plan to Become a Better Developer #120DaysBetterDev

My GSoC 2021 journey with SCoRe Lab

Professional IT Outstaffing Services: How we build an outsourced DevOps team

Building Google Contacts Screen and its Scrolling Bubble Feature - in Compose

Functional verification test framework with Go

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nico Anastasio

Nico Anastasio

An Italian coder and design enthusiast. I publish content on my blog that helps web developers improve their skills and share knowledge. http://anastasionico.uk

More from Medium

An Ubuntu Environment with Node.js

Payment Gateway Integration to Django-Angular

Why Should You Use Node JS ?

How does NodeJS require modules?

how does nodejs require modules — directory structure