OpenLampTech

Share this post

PHP array_diff() function - A handy use case learned

openlamptech.substack.com

PHP array_diff() function - A handy use case learned

As I continue to learn PHP development, I'm sharing those concepts with readers here. This quick post covers a handy use of the PHP array_diff() function I learned about...

Joshua Otwell
Feb 15, 2022
Share this post

PHP array_diff() function - A handy use case learned

openlamptech.substack.com

I recently used the PHP array_diff() function in some prototype scripts and wanted to share this specific use case example for others interested…

For seasoned PHP developers, this likely isn’t anything new or shiny as it is with me, so feel free to disregard my exuberance.


Thanks for reading OpenLampTech! Subscribe to the free weekly newsletter for all the fantastic original and curated PHP/MySQL content I share in the publication.


PHP scandir() function and dotfiles

Typically when you use the scandir() function, there are a couple of dotfiles (. and ..) that are returned as part of the array of files in the target directory.

📝 Note: I am terming them dotfiles here. They may have an official name (that I do not know) so take this term loosely. Feel free to share with me the official name (if there is one). Maybe they represent the hidden files in a directory?

While working on CSV upload functionality, I wanted a way to filter out those 2 dotfiles from the array returned by scandir(), in order to work with just the CSV files.

PHP array_diff() function - removing the dotfiles

Here's what I came up with using array_diff()…

It is simplistic yet effective.

array_diff() syntax is as follows (from the official PHP documentation):

array_diff(array $array, array ...$arrays): array

Support OpenLampTech, my blog, and my work with a coffee if you would like.☕


Below is simple code I use for the example:

$all_files = scandir('/opt/lampp/htdocs/writing_projects/files/');

foreach ($all_files as $file) {
	
	echo '<strong>'.$file.'</strong>'.'<br>';
}

Looping through the $all_files array, we can see there are 5 files total: 2 dotfiles and 3 CSV files:

We can remove the 2 dotfiles using array_diff() like this:

$non_dotfiles = array_diff($all_files, array('.', '..'))

Do you need to learn MySQL? Just starting out and confused about how to query a table and get the data you need? I am creating premium MySQL Beginners content for this specific reason. To help those who want to learn the basics of MySQL but don't know where to start. Learn more about my premium MySQL blog posts and content as I develop and release them.

Tell me more!


When looping through the $non_dotfiles array, all files are present with the exception of the 2 dotfiles:

foreach ($non_dotfiles as $file) {
	
	echo '<strong>'.$file.'</strong>'.'<br>';
}

I won’t lay claim this is the best and only way to remove the dotfiles from a returned scandir() array or that this is the only use for array_diff(). Just something I cobbled together that works I wanted to share.

Share with me other ways you have used array_diff() in your code, along with workarounds for the dotfiles in scandir() arrays…

Similar Content

OpenLampTech
OpenLampTech issue #12 - My RTC Interview
Hey glad to have you here! 👍 If someone shared this newsletter with you and you are not yet subscribed, please use the Subscribe button below and join:Thanks for reading OpenLampTech! Subscribe (it’s free) and receive new posts by email, be part of a great community, and support my work…
Read more
a year ago · Joshua Otwell
OpenLampTech
MySQL ROW_NUMBER() Window Function with CASE Expression
In this post, I am covering how to use the MySQL ROW_NUMBER() window function and an CASE expression to collapse multiple rows of data into a single row per group…
Read more
a year ago · Joshua Otwell
OpenLampTech
OpenLamp.tech Issue #7 - MySQL Metadata with CodeIgniter 4
Hey glad to have you here! 👍 If you are not yet subscribed, please use the Subscribe button below and join:Thanks for reading OpenLamp.tech! Subscribe (it’s free) and receive new posts by email, be part of a great community, and support my work…
Read more
a year ago · Joshua Otwell

Visit my blog Digital Owl’s Prose, where I write regularly on CodeIgniter, PHP, and SQL. I’m also active on Medium, LinkedIn, Twitter, and Instagram.


If you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate content and are much appreciated.

Thank you for reading!!!

Thank you for reading OpenLampTech. This post is public so feel free to share it. Thank you! 🙏

Share


How can I help you?
  • Are you thinking of starting up a blog? I use WordPress for my blog, Digital Owl’s Prose. Let’s both save money on the plans offered. 💸

  • Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. ✉️

  • Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.

  • I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.

  • Grab a free pack of mobile Creator wallpapers with my referral link.

  • Just getting started or wanting to learn MySQL? Find out about my premium blog posts and MySQL Beginner Series here.


Want to support this newsletter and my work? Drop some spare change in my Tip Jar.💰

Tip Jar

Disclosure: Some of the services and products links in this email are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Cover photo credits: Image by Mateusz Zdrzałek from Pixabay

Share this post

PHP array_diff() function - A handy use case learned

openlamptech.substack.com
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 Joshua Otwell
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing