Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created February 28, 2012 01:47
Show Gist options
  • Save mhulse/1928452 to your computer and use it in GitHub Desktop.
Save mhulse/1928452 to your computer and use it in GitHub Desktop.
PHP test array example...
<?php
/**
* Inserts values into an array after a given key.
*
* Values from $insert are inserted after (or before) $key in $array. If $key
* is not found, $insert is appended to $array using array_merge().
*
* @param $array
* The array to insert into. Passed by reference and altered in place.
* @param $key
* The key of $array to insert after.
* @param $insert
* An array whose values should be inserted.
* @param $before
* If TRUE, insert before the given key, rather than after it.
* Defaults to inserting after.
* @author: Shawn M.
* @link: http://marc.info/?l=php-general&m=133044704602038&w=2
*/
function array_insert(&$array, $key = '', $insert, $before = FALSE) {
if (is_array($array)) { // Overkill?
if (array_key_exists($key, $array)) {
$i = 0;
foreach($array as $k => $v) {
if($k === $key) {
$p = ($before) ? $i - 1 : $i + 1;
break;
}
$i++;
}
$array = array_merge(array_slice($array, 0, $p), $insert, array_slice($array, $p, count($array) - $p));
} else {
$array = array_merge($array, $insert);
}
}
}
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type:text/plain');
$o_insert['new'] = array(
'mickey' => 'mouse',
'minnie' => 'mouse',
'donald' => 'ducks',
'daffy' => 'duck',
);
$o = array(
'animals' => array(
'dogs' => 'bark',
'cats' => array (
'sound1' => 'pur',
'sound2' => 'meow',
),
),
'misc' => array(
'baz' => array(
'fun' => 'times',
'good' => 'days',
'almost' => 'over',
),
'foo' => array(
'what' => 'tf',
'this' => 'is it',
'bah' => 'humbug',
),
#
#
# -----> I want to insert $o_insert here! <-----
#
#
'bar' => array(
'who' => array(
'deep' => array(
'here' => 'goodbye',
),
),
),
),
'materials' => array(
'car' => 'mazda',
'house' => array(
'stories' => 2,
'beds' => 3,
'baths' => 2,
),
),
'work' => array(
'company' => 'Acme Inc.',
'management' => array(
'boss' => 'billy',
'ceo' => 'suzy',
'cfo' => 'betty',
),
),
);
# I've tried both:
#
# http://eosrei.net/articles/2011/11/php-arrayinsertafter-arrayinsertbefore
# http://drupal.org/node/66183
#
/**
* Inserts values into an array after a given key.
*
* Values from $insert_array are inserted after (or before) $key in $array. If
* $key is not found, $insert_array is appended to $array using array_merge().
*
* @param $array
* The array to insert into. Passed by reference and altered in place.
* @param $key
* The key of $array to insert after
* @param $insert_array
* An array whose values should be inserted.
* @param $before
* If TRUE, insert before the given key, rather than after it.
* Defaults to inserting after.
*/
function array_insert(&$array, $key, $insert_array, $before = FALSE) {
$done = FALSE;
foreach ($array as $array_key => $array_val) {
if (!$before) {
$new_array[$array_key] = $array_val;
}
if ($array_key == $key && !$done) {
foreach ($insert_array as $insert_array_key => $insert_array_val) {
$new_array[$insert_array_key] = $insert_array_val;
}
$done = TRUE;
}
if ($before) {
$new_array[$array_key] = $array_val;
}
}
if (!$done) {
$new_array = array_merge($array, $insert_array);
}
// Put the new array in the place of the original.
$array = $new_array;
}
$o_key = $o['misc'];
array_insert($o_key, 'foo', $o_insert);
$o['misc'] = $o_key;
print_r($o);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment