Wednesday, 31 December 2014

PHP Array Functions

 PHP Array Functions



Array Merge array_merge() id used to merge the elements of one or more than one array in such a way  that the value of one array appends at the end of first array. If the any of the arrays have same key ,then the later value overrides the previous value for that key. The output of the function is one resultant array and this function takes two arrays as input.

Example:
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);

print_r($c);

Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 ) 

Array Combinearray_combine() is used to creates a new array by using the key of one array as keys and using the value of other array as values.One thing to keep in mind while using array_combine() that number of values in both arrays must be same.
Example:
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_merge($fname,$age);
print_r($c);

Output
Array ( [0] => Peter [1] => Ben [2] => Joe [3] => 35 [4] => 37 [5] => 43 ) 

NB. Both parameters should have an equal number of elements

Array Split
Used to split array,

Example:
$ip = "123.456.789.000"; // some IP address
$iparr = split ("\.", $ip); 
print "$iparr[0] <br />";
print "$iparr[1] <br />" ;
print "$iparr[2] <br />"  ;
print "$iparr[3] <br />"  ;

Output:
123
456
789
00
0

Array Slice
To extract only a subset of the array, use the array_slice( ) function:
array_slice(array,start,length,preserve)


The array_slice( ) function returns a new array consisting of a consecutive series of values from the original array. The offset parameter identifies the initial element to copy (0 represents the first element in the array), and the length parameter identifies the number of values to copy. The new array has consecutive numeric keys starting at 0. For example:

Example:
$people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo');
$middle = array_slice($people, 2, 2);
print_r($middle);

Output:
Array ( [0] => Harriet [1] => Brenda ) 

Combine array_slice( ) with list( ) to extract only some values to variables:

Example:
$order = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo');
$te = list($second, $third) = array_slice($order, 1, 2);
print_r($te);

Output:
Array ( [0] => Dick [1] => Harriet ) 

Array Chunks
The function returns an array of the smaller arrays. The third argument, preserve_keys, is a Boolean value that determines whether the elements of the new arrays have the same keys as in the original (useful for associative arrays) or new numeric keys starting from 0 (useful for indexed arrays).

Example:
$fname=array("Peter","Ben","Joe","Hasd","Mam","Teat","Punk");
$chunk = array_chunk($c,2);
print_r($chunk);

Output:
Array ( [0] => Array ( [0] => Peter [1] => Ben ) [1] => Array ( [0] => Joe [1] => Hasd ) [2] => Array ( [0] => Mam [1] => Teat ) [3] => Array ( [0] => Punk ) ) 

Array Splice
The array_splice( ) function can remove or insert elements in an array

Example:
$subjects = array('physics', 'chem', 'math', 'bio', 'cs', 'drama', 'classics');
$removed = array_splice($subjects, 2, 3);
print_r($subjects);
print_r($removed);

$subjects1 = array('physics', 'chem', 'math', 'bio', 'cs', 'drama', 'classics');
$removed1 = array_splice($subjects1, 2);
print_r($subjects1);
print_r($removed1);


Output:
Array ( [0] => physics [1] => chem [2] => drama [3] => classics ) 
Array ( [0] => math [1] => bio [2] => cs )
Array ( [0] => physics [1] => chem )
Array ( [0] => math [1] => bio [2] => cs [3] => drama [4] => classics ) 


Array Element Exists
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
array_key_exists(key,array)


No comments:

Post a Comment