note 94107 added to function.in-array

From: Date: Fri, 16 Oct 2009 16:04:30 +0000
Subject: note 94107 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-161813@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 65.60.87.114 ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/94107 Del: integrated -- https://master.php.net/note/delete/94107/integrated Del: useless -- https://master.php.net/note/delete/94107/useless Del: bad code -- https://master.php.net/note/delete/94107/bad+code Del: spam -- https://master.php.net/note/delete/94107/spam Del: non-english -- https://master.php.net/note/delete/94107/non-english Del: in docs -- https://master.php.net/note/delete/94107/in+docs Del: other reasons-- https://master.php.net/note/delete/94107 Reject -- https://master.php.net/note/reject/94107 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161813) next »