note 94107 deleted from function.in-array by bjori

From: Date: Fri, 01 Jan 2010 17:33:59 +0000
Subject: note 94107 deleted from function.in-array by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-164518@lists.php.net to get a copy of this message
Note Submitter: micah at thisisnt dot com ---- Needed to insert values at a specific point in another array, so I built this, it's untested on multidimensional arrays, but it works for my purposes: <?php /** inserts an array at a specific point in the other array. If you omit $after, it acts just like array merge. * @param array $array array to merge into * @param mixed $insert array or value to insert (non-array value is merged as array($value) * @param string $after key to insert after. $key must be a valid key in $array or will default to merging at end like array_merge() * @return array returns new array after merge/insert */ function array_insert($array, $insert, $after = '') { if (!array_key_exists ($after, $array)) { return array_merge($array, $insert); } $new = array(); $lastkey = ''; foreach ($array as $key=>$value) { if ($after == $lastkey) { $new = array_merge($new, (array) $insert); } $new[$key] = $value; $lastkey = $key; } return $new; } ?>

« previous php.notes (#164518) next »