PHP Arrays and Effective Way of Working


PHP Array

Today I will enlist common PHP array functions with proper example for effective usage and best practices. It is imperative for every PHP developer to know their usage as well as combining array functions to make code short yet readable.

 

1. The Basics
Considering the basic functions that work fine with array keys and values. One of them is array_combine(), use to create an array using one array for keys and another for its values:

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];
$array = array_combine($keys, $values);
print_r($array);

// Array
// (
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// )

 

Here, the function array_values() returns an indexed array of values, array_keys() returns an array of keys of a given array, and array_flip() exchanges keys with the values:

 

2. Code Shortening
The function list(), which is less a function, rather its more a language construction, is designed to assign variables in a short way. For example,below example showcase the usage of the list() function:

 

// define array
$array = ['x', 'y', 'z'];
// without list()
$a = $array[0];
$b = $array[1];
$c = $array[2];

 // with list()
list($x, $y, $z) = $array;

 
This construction works smoothly with functions like preg_slit() or  explode() . Also, you can skip some parameters, if you don’t want them to be defined:
 

$string = ‘hello|woderful|world’;
list($hello, , $world) = explode(‘|’, $string);
echo(“$hello, $world”); // hello, world

 

The list() can also be used with foreach, which will boost the effectiveness of this construction

 

$arrays = [[1, 2], [3, 4], [5, 6]]:
foreach ($arrays as list($a, $b)) {
$c = $a + $b;
echo($c . ‘, ‘); // 3, 7, 11,
}

 

With the help of extract() function, you can export an associative array to variables. For each element of an array, a variable will be created with the name of a key and value set as a value of the element:

 

$array = [
   'clothes' => 'shirt',
   'size'    => 'large',
   'color'   => 'red',
];

 extract($array);

 echo(“$clothes $size $color”); // shirt large red

 

Keep in mind that extract() is not safe while working with user data (like results of requests), so it is advisable to use this function with the flags EXTR_IF_EXISTS and EXTR_PREFIX_ALL. Compact() function is the opposite of the previous function, it makes an associative array from variables:

 

$clothes = ‘shirt’;
$size = ‘large’;
$color = ‘red’;

$array = compact(‘clothes’, ‘size’, ‘color’);
print_r($array);

// Array
// (
//     [clothes] => shirt
//     [size] => large
//     [color] => red
// )

 

3. Joining up the Arrays

The popular and impactful  method to merge two or more arrays in PHP is by using the array_merge() function. This will merge the items of both arrays and values will be merged with similar string keys will be overwritten with the last value.

 

$array1 = ['a' => 'a', 'b' => 'b', 'c' => 'c'];
$array2 = ['a' => 'A', 'b' => 'B', 'D' => 'D']; 

$merge = array_merge($array1, $array2);
print_r($merge);
// Array
// (
//     [a] => A
//     [b] => B
//     [c] => c
//     [D] => D
// )

 

In order to remove array values from another array (or arrays), go for array_diff(). For getting values which are present inside the given array, use array_intersect(). The below example will demonstrate it’s  working:

 

$array1 = [1, 2, 3, 4];
$array2 =       [3, 4, 5, 6];

$diff = array_diff($array1, $array2);
print_r($diff); // [0 => 1, 1 => 2]

$intersect = array_intersect($array1, $array2);
print_r($intersect);  // [2 => 3, 3 => 4]

 

 Looking forward for your comment and query regarding what are your views wrt working with arrays in PHP.

 

About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile applicationWeb application or an E-commerce solution

 
You may be interested in following:

  1. Importance of Prevent SQL Injection in PHP
  2. Twitter Streaming API in PHP Complete Guide
  3. PHP Image Manipulation Step by Step and Easy