Home » Blog » Information Technology » Arrays in PHP

Arrays in PHP

About the Array function in PHP:

Array in the 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.

What is mean by 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.

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.

Multidimensional array is 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.

Handling of 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 again. Each of these functions will also handle multidimensional arrays at any depth.

How to find the value of a Key?

First Step

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 seek

Key($haystack, $needle)

{

foreach($haystack as $key => $value)

{

if($key == $needle)

{

$output = $value;

}

else

if(is_array($value))

{

$output = seek($value, $needle);

}

}

return $output;

}

?>

Find the Key that matches the Value

Second Step

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 seek

Value($haystack, $needle)

{

foreach($haystack as $key => $value)

{

if($key == $needle)

{

$output = $value;

}

else

If

(is_array($value))

{

$output = seek($value, $needle);

}

}

return $output;

}

?>

Destroy Key and Value

Third Step

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);

}

else

if(is_array($value))

{

$output[$key] = seekAndDestroy($value, $needle);

}

else

{

$output[$key] = $value;

}

}

return $output;

}

?>

Rename Key

Final Step

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;

}

else

{

if(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.

 

Leave a Reply