Arrays in PHP are one of the fundamental building blocks that allow you to handle data and use it intelligently within your application. Used correctly, they allow you as the developer to handle large amounts of data efficiently and with ease. However Arrays can quickly become complicated once you start creating multidimensional arrays. A multidimensional array is an array within an array. Arrays can be built within each other recursively to handle large, complex sets of data.

After finding that I had to deal with multidimensional arrays quite frequently, I decided to write a couple of functions to handle common tasks.

So here are a couple of functions to handle some common tasks if you are working with multidimensional arrays.

What is an array?

PHP is often one of the first languages people start exploring once they get interested in server-side development because it is so prevalent on the Internet. I’m sure there will be a lot of newbies out there who are trying to make working with multidimensional arrays easier to understand. With that in mind, first I’ll explain exactly what a multidimensional array is. If you are already familiar with multidimensional arrays, feel free to skip this section.

If you had to Google multidimensional arrays, I’ll assume you are familiar with saving variables in PHP.

For example, to save the name of a cat, you might write the following:

<?php  
$cat = "Percy";  
?>  

This would save the name Percy into the variable $cat.

If you think of a variable as a box, then you can put one thing into the box. An array allows you to put multiple things into the same box. For example:

<?php  
$cat = array(  
"Name" => "Percy",  
"Colour" => "Black"  
);  
?>  

In this example we created a cat, give it a name and a colour. Once you start to really getting into PHP development, you will find arrays are far more useful than variables.

A multidimensional array then, is simply an array within an array. This allows you to save multiple bits of data in a logical structure. For example:

<?php  
$cat = array(  
"Name" => "Percy",  
"Colour" => "Black",  
"Hobbies" => array(  
1 => "Chasing mice",  
2 => "Sleeping",  
3 => "Prowling"  
),  
);  
?>  

An array is made up of keys and values. The key is the bit on the left, for example, “Name”, and the value is the bit on the right, for example, “Percy”.

To create a multidimensional array you simply create a new array within the value of a key.

When dealing with arrays in PHP, you can print the keys and values out like this:

<?php  
echo "<pre>";  
print_r($cat);  
echo "</pre>";  
?>  

Note, the pre HTML tags are used to display the array in a more readable format on the page.

I hope that gives a good overview of what is a multidimensional array. To put it simply, it is really just a way of storing multiple bits of data that are grouped together into key and value pairs.

Multidimensional array functions

If you have ever built a large-scale system in PHP, you will know the pain of having to deal with multidimensional arrays to complete all sorts of tasks. Handling these arrays can often get complicated, and so I’ve found myself constantly using the following functions time and time again.

None of these functions are complicated, and they all follow the same structure, but I find them easy to remember and use whenever I need them. Each of these functions will also handle multidimensional arrays at any depth.

Find the value of a Key

The first function will find the Value from a Key. This is useful when you are searching an array for a specific bit of data, such as a name or an email address.

<?php  
// Find the value of a Key  
function seekKey($haystack, $needle){  
foreach($haystack as $key => $value){  
if($key == $needle){  
$output = $value;  
}elseif(is_array($value)){  
$output = seek($value, $needle);  
}  
}  
return $output;  
}  
?>  

Find the Key that matches the Value

The second function finds the Key that matches the Value. This is useful when you already have the Value, but you also need the Key.

<?php  
// Find the Key that matches the Value  
function seekValue($haystack, $needle){  
foreach($haystack as $key => $value){  
if($key == $needle){  
$output = $value;  
}elseif(is_array($value)){  
$output = seek($value, $needle);  
}  
}  
return $output;  
}  
?>  

Destroy Key and Value

The third function will destroy a Key => Value pair by unsetting it from the array. This is obviously useful when you want to quickly delete something from an array but it could be present at any level.

<?php  
// Destroy Key and Value  
function seekAndDestroy($haystack, $needle){  
foreach($haystack as $key => $value){  
if($key == $needle){  
unset($key);  
}elseif(is_array($value)){  
$output[$key] = seekAndDestroy($value, $needle);  
}else{  
$output[$key] = $value;  
}  
}  
return $output;  
}  
?>  

Rename Key

And finally, this function will rename a Key. This is useful if you are transferring data between systems or databases that use different field names.

<?php  
// Rename Key  
function seekAndRename($haystack, $needle, $new){  
foreach($haystack as $key => $value){  
if($key == $needle){  
$output[$new] = $value;  
}elseif(is_array($value)){  
$output[$key] = seekAndRename($value, $needle, $new);  
}else{  
$output[$key] = $value;  
}  
}  
return $output;  
}  
?>  

I hope you found this useful and you are able to plug these functions into your website or web app to make handling multidimensional arrays easier.

Do you have any other multidimensional arrays tasks you need help with? Leave a comment and I’ll help you write a function to deal with them.