Here’s a code snippet to flatten or merge multidimensional array. This is useful when we need to find if a value exists in any of the nodes in a multidimensional array.

I use this function in my Paid Content Packages WordPress Plugin to find out if any of the packages have any page or post assigned.

PHP Function:

function flattenArray($arrayToFlatten) {
	$flatArray = array();
	foreach($arrayToFlatten as $element) {
		if (is_array($element)) {
			$flatArray = array_merge($flatArray, flattenArray($element));
		} else {
			$flatArray[] = $element;
	return $flatArray;


$array = array(
	'parent-one' => 'parent-one',
	'parent-two' => 'parent-two',
	'parent-three' => array(
		'child-one' => 'child-one',
		'child-two' => 'child-two',
		'child-three' => array(
			'kid-one' => 'kid-one',
			'kid-two' => 'kid-two',

This code will print the following output.

    [0] => parent-one
    [1] => parent-two
    [2] => child-one
    [3] => child-two
    [4] => kid-one
    [5] => kid-two

There are shorter versions of this function available, however, I like to use the code which is clear and easy to read. Hope this helps you if you are finding a solution to this.

Mohit Aneja
I am an experienced designer and developer with a solid knowledge & experience of current web and mobile technology. I am a problem solver at heart and strive to develop the most effective solutions at the highest of standards and current technology.

  • Stay up-to-date!
    Join our mailing list to receive product updates & announcements. Every month we give away FREE Licenses to our lucky subscribers.

    No Spam Ever!

Stay up-to-date!

Get notified of product updates, new products and freebies.