CodingBison

The last set of array-based functions present functions that facilitate miscellaneous aspects of array handling. These functions are: array_pad(), array_slice(), array_chunk(), array_keys(), array_values(), and array_key_exists().

We begin with their signature:

 $new_padded_array = array_pad($array, $total_length, $padding_value);
 $new_sliced_array = array_slice($array, $start_offset, $length);
 $array_chunks     = array_chunk($array, $chunk_length);
 $array_of_keys    = array_keys($passed_array);
 $array_of_values  = array_values($passed_array);
 $value_exists     = array_key_exists($search_value, $passed_array);

Let us describe these functions.

array_pad() allows us to pad an array by adding newer elements to the array. This function takes optional arguments of total length and padding value. If the length of the array is less than the total length passed, then array_pad() adds the padding value (passed as an argument) as new elements towards the end of the array -- this way, the total length becomes same as the passed length. On the other hand, if the optional total length passed is less than the length of the array, then no padding is done. Also, if the $total_length parameter is negative, then padding is done on the left side of the array. Once again, the absolute value of negative $total_length must be greater than the array length, otherwise no padding is done.

The next function, array_slice(), slices an array and returns a sliced array from the main (larger) array. It takes an offset argument and an optional length parameter -- the sliced array starts at the offset and then include elements till the optional length is reached (or remaining length from the offset, if the optional length is not specified).

Here is a description of the remaining four functions from the above list. array_chunks() splits a bigger array into chunks of smaller arrays. The function array_keys() returns an array that has all the keys of the associative array. The next function, array_values(), returns an array that has all the values of the associative array. Lastly, array_key_exists() function checks if a given key is present in the array or not.

We present two small programs that provide usage of these functions. The first program describes array_pad(), array_slice(), and array_chunk().

 <?php
 $array_userids = array("jfulton", "tkipling", "ksimon");
 echo "Original Array: <br>";
 print_r($array_userids);

 $array_userid_padded = array_pad($array_userids, 6, "N.A.");
 echo "<br><br>Padded Array: <br>";
 print_r($array_userid_padded);

 echo "<br><br>Padded Array (with negative length): <br>";
 $array_userid_padded = array_pad($array_userids, -6, "N.A.");
 print_r($array_userid_padded);

 $array_more_userids = array("jfulton", "tkipling", "ksimon", "msimpson", "hjones");
 $arr_sliced = array_slice($array_more_userids, 2, 3); 
 echo "<br><br>Sliced Array: <br>";
 print_r($arr_sliced);

 $array_total_userids = array("jfulton", "tkipling", "ksimon", "msimpson", "hjones", "amoore");
 $array_chunks = array_chunk($array_total_userids, 2); 
 echo "<br><br>Array chunks: <br>";
 foreach ($array_chunks as $elements) {
     print_r($elements);
     echo "<br>";
 }
 ?>

Here is the output the the first program:

 Original Array:
 Array ( [0] => jfulton [1] => tkipling [2] => ksimon )

 Padded Array:
 Array ( [0] => jfulton [1] => tkipling [2] => ksimon [3] => N.A. [4] => N.A. [5] => N.A. )

 Padded Array (with negative length):
 Array ( [0] => N.A. [1] => N.A. [2] => N.A. [3] => jfulton [4] => tkipling [5] => ksimon )

 Sliced Array:
 Array ( [0] => ksimon [1] => msimpson [2] => hjones )

 Array chunks:
 Array ( [0] => jfulton [1] => tkipling )
 Array ( [0] => ksimon [1] => msimpson )
 Array ( [0] => hjones [1] => amoore )

The second program covers the remaining functions of array_keys(), array_values(), and array_key_exists().

 <?php
 $arr_users = array('user_name' => "jwayne", 'user_zipcode' => 10110, 'user_city' => "Palo Alto");
 echo "Let us print key and values: <br>";
 print_r($arr_users);

 echo "<br><br> Let us print all keys: <br>";
 $arr_keys = array_keys($arr_users);
 print_r($arr_keys);

 echo "<br><br> Let us print all values: <br>";
 $arr_values = array_values($arr_users);
 print_r($arr_values);

 $username_exists = array_key_exists("user_name", $arr_users);
 $userpassword_exists = array_key_exists("user_password", $arr_users);
 echo "<br><br>username_exists: ";
 var_dump($username_exists);
 echo "<br>userpassword_exists: ";
 var_dump($userpassword_exists);
 ?>

We provide the output below. As expected, array_keys() returns an array that holds all the keys and array_values() returns an array that holds all the values. Lastly, as expected, array_key_exists() return true if a key exists, else it returns false.

 Let us print key and values:
 Array ( [user_name] => jwayne [user_zipcode] => 10110 [user_city] => Palo Alto )

 Let us print all keys:
 Array ( [0] => user_name [1] => user_zipcode [2] => user_city )

 Let us print all values:
 Array ( [0] => jwayne [1] => 10110 [2] => Palo Alto )

 username_exists: bool(true)
 userpassword_exists: bool(false) 




comments powered by Disqus