Today, i like to share two handy array functions in PHP, which are used to insert array before/after the key into the existing array.
Insert array before function
This function will insert key/value as well as array before the key in the existing array.
This function will insert key/value as well as array after the key in the existing array.
Insert array before function
This function will insert key/value as well as array before the key in the existing array.
/** * Inserts a new key/value before the key in the array. * * @param $key The key to insert before. * @param $array An array to insert in to. * @param $new_key The key/array to insert. * @param $new_value An value to insert. * @return array */ public function array_insert_before($key, array $array, $new_key, $new_value = null) { if (array_key_exists($key, $array)) { $new = array(); foreach($array as $k = > $value) { if ($k === $key) { if (is_array($new_key) && count($new_key) > 0) { $new = array_merge($new, $new_key); } else { $new[$new_key] = $new_value; } } $new[$k] = $value; } return $new; } return false; }
Inorder to insert key/value pairs before the key into the array, Here is the PHP code
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'age'=>29, 'dob'=>'29-08-1983' ); $result = array_insert_before('age',$original, 'country', 'india');
Above method will insert 'country' key with value 'india' into the $original array before the 'age' array key. So the final output for $result is
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'country'=>'India', 'age'=>29, 'dob'=>'29-08-1983' );
Inorder to insert arrays before the key into the existing array, Here is the PHP code
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'age'=>29, 'dob'=>'29-08-1983' ); $insert = array( 'country'=>'india', 'state'=>'Tamil Nadu' ); $result = array_insert_before('age',$original, $insert) ;
Above method will insert $insert array values into the $original array before the 'age' array key. So the final output for $result is
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'country'=>'India', 'state'=>'Tamil Nadu', 'age'=>29, 'dob'=>'29-08-1983' );
Insert array after function
This function will insert key/value as well as array after the key in the existing array.
/** * Inserts a new key/value after the key in the array. * * @param $key The key to insert after. * @param $array An array to insert in to. * @param $new_key The key/array to insert. * @param $new_value An value to insert. * * @return array */ public static function array_insert_after($key, array $array, $new_key, $new_value = null) { if (array_key_exists($key, $array)) { $new = array(); foreach($array as $k = > $value) { $new[$k] = $value; if ($k === $key) { if (is_array($new_key) && count($new_key) > 0) { $new = array_merge($new, $new_key); } else { $new[$new_key] = $new_value; } } } return $new; } return false; }
Inorder to insert arrays after the key into the existing array, Here is the PHP code
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'age'=>29, 'dob'=>'29-08-1983' ); $insert = array( 'country'=>'india', 'state'=>'Tamil Nadu' ); $result = array_insert_after('age',$original, $insert) ;
Above method will insert $insert array values into the $original array after the 'age' array key. So the final output for $result is
$original = array( 'firstName'=>'Suresh', 'lastName'=>'Kumar', 'age'=>29, 'country'=>'India', 'state'=>'Tamil Nadu', 'dob'=>'29-08-1983' );
Hope, you enjoyed this Post.
No comments:
Post a Comment