PHP: remove duplicate elements from a multi-dimensional array

This post is over 3 years old, so please keep in mind that some of its content might not be relevant anymore.

PHP’s array_unique() is great for removing duplicates from a flat array, but it chokes on multi-dimensional arrays. If you have nested data and need to remove duplicates, here’s a clean one-liner that does the job.

The trick: serialize, unique, unserialize

The idea is straightforward: flatten the array temporarily by serialising each element, let array_unique() do its thing on the flat list, then unserialise everything back to its original structure.

$uniqueArray = array_map("unserialize", array_unique(array_map("serialize", $array)));

Working from the inside out:

  • array_map("serialize", $array) — turns each nested element into a string representation
  • array_unique() — removes duplicate strings (which means duplicate nested structures)
  • array_map("unserialize", ...) — restores each string back to its original PHP value

The result is a multi-dimensional array with all duplicate entries removed, regardless of how deep the nesting goes. You keep the original structure and no external libraries needed.

Hope it helps!

Drush intro – Drupal

Drush is a command-line shell interface for Drupal. It lets you do almost anything you would do through the Drupal admin interface — enabling modules, clearing caches, running cron, backing up the site — all from the terminal.

Installation

The preferred way to install Drush is via the official PEAR channel, but it can also be installed manually on a shared server.

Backup and restore

archive-dump backs up the codebase, files, and database into a single file:

drush archive-dump
drush archive-restore

Common commands

drush pm-download 
drush pm-enable 
drush pm-disable 
drush cc all
drush cron
drush sql-dump
drush uli

Enjoy!

PHP UK Conference 2012 slides – London

As mentioned in the last post, I was going to go to the PHP Conference in London.
Well, I have been! :-)
It was the first time for me but was exactly as expected. Very interesting, entertaining, and absolutely inspiring…
Unfortunately the slides are not always easy to find as I haven’t found a single place where to find them all.
Here I will list all slides (or resources in general) found about a particular talk.
If you know where to find some of those marked as “not found yet” don’t hesitate to write to andrea [at] opensourceisbetter.com or leave a comment.

A look at PHP in 2012
(Rasmus Lerdorf)
Slides

Big Data web – Big Data science – Big Data mining
(James Littlejohn)
Slides

Powering your website with real-time data
(Bert Van Hauwaert)
Slides

The journey towards Continuous Integration
(Sebastian Marek)
Slides

Data abstraction in large web applications
(Brandon Savage)
Slides

Develop & deploy using hybrid cloud strategies
(Thijs Feryn)
Slides

PHP 5.4: the new bits
(Davey Shafik)
Slides

Creative Coding – why “doing nothing” doesn’t mean you’re not working
(June Henriksen)
Slides

Security audits as an integral part of PHP application development
(Sijmen Ruwhof)
Slides

To a thousand servers and beyond: scaling a massive PHP application
(Nikolay Bachiyski)
Slides

Distribute the workload
(Helgi Þorbjörnsson)
Slides – Day 1Slides – Day 2

Profiling PHP applications
(Derick Rethans)
Resources

Scaling communication with Continuous Integration
(LB Denker)
Slides

Building and deploying applications with Phing
(Michiel Rook)
Slides

Teaching your machine to find fraudsters
(Ian Barber)
Slides

Try {getting people to come to a talk about exceptions}
(Ed Van Beinum)
Slides

Challenges at scale: extreme data and platforms at eBay
(Hugh Williams)
(not found yet)

Masterizing PHP Data Structure 102
(Patrick Allaert)
Slides

Monitoring your back-end for speed and profit
(Andy ‘Bob’ Brockhurst)
Resources

Open source search: an analysis
(Justin Finkelstein)
Slides

Cheat your way with UX
(Stephanie Troeth)
Slides

MySQL update
(Dave Stokes)
(not found yet)

Recognizing smelly code
(Harrie Verveer)
Slides – PDF file

HTML5 for PHP developers
(Andrew Betts)
(not found yet)

Introduction: MongoDB with PHP
(Derick Rethans)
Resources

PHP under the hood
(Johannes Schlüter)
Slides

The Misguided Manager
(Zoë Slattery)
Slides

Hope you find this useful. :-)
Enjoy!

PHP UK Conference 2012 schedule – London

This year, for the first time, I will be attending to the PHP Conference in London. There are going to be so many interesting talks.
Have a look with your eyes..
Here you find the official schedule. Unfortunately it’s still provisional.

Anyway I took the time to create a simple spreadsheet to organize, with my colleagues, what talks we will be attending.
Here you can download it: PHP UK Conference 2012 Schedule Spreadsheet.

For those of you who haven’t registered to the PHP conference yet, you can register with a significant discount, instead of the original price, thanks to the PHP London meetup.

I’ll see you there! :-)

Enjoy the conference! :-)

How to run a system command in background from PHP

If you are at the command line and want a process to run in the background, you add an & at the end of the command:

sleep 10 && echo "Done!" &

The same approach works from PHP when you need to run a long process without making the user wait:

exec("tar czf /backup/site.tar.gz /var/www > /dev/null 2>&1 &");

The > /dev/null 2>&1 discards output so PHP doesn’t wait for it. The & sends the process to the background.

PHP functions compared

  • exec() — returns last line of output, does not block if output is redirected
  • shell_exec() — returns full output as a string
  • system() — outputs directly to the browser
  • passthru() — for binary output

Using nohup and PIDs

exec("nohup tar czf /backup/site.tar.gz /var/www > /dev/null 2>&1 & echo $!", $output);
$pid = $output[0];

nohup keeps the process alive after PHP exits. The PID lets you monitor or kill it later.

Enjoy!

IDE Netbeans 7.0 is out: PHP, Java SE/EE and C/C++

Netbeans 7 is out. I have been using it for PHP, at work as well as at home, for almost two years and I have to say it has become by far my favourite PHP IDE.

The Netbeans IDE started in 1996 for Java. PHP support came with version 6.5 in late 2008. Since then, every release has improved significantly.

What I like about it is that it just works out of the box — the PHP code completion, the debugger integration with Xdebug, the SVN/Git support built right in. You don’t need to piece together half a dozen plugins to get a productive environment.

One of my favourite features is the ability to search across files with Alt+Shift+O. It’s one of those things you don’t appreciate until you try going back to an IDE that doesn’t have it.

Curious about it? Download the version you need from the Netbeans site and give it a try.

Enjoy it!

How to configure sendmail as a SMART HOST

Sometimes I need to test the email functionality of a PHP website on my local machine. The best and easiest way I found so far is using sendmail as a smart host.

A smart host is a piece of software that routes email to an intermediate mail server rather than directly to the recipient’s server. The only thing you need is the SMTP address of your internet service provider. For example, my ISP is Virgin Media and their SMTP server is smtp.virgin.net.

First of all you have to install sendmail. On Ubuntu:

sudo apt-get install sendmail

Then open your configuration file:

sudo vi /etc/mail/sendmail.mc

and append or modify the following line to define your ISP SMTP server:

define(`SMART_HOST',`smtp.virgin.net')

Remove the “dnl” word at the end if there is one. Now login as root and regenerate the sendmail config file:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

and restart sendmail:

service sendmail restart

Now your local machine should be able to send emails. The easiest way to test is from the PHP interactive shell:

php -a
mail('myemail@domain.com','TEST','WORKING?!?');

Enjoy… :-)