note 86273 added to function.array-push

From: Date: Fri, 10 Oct 2008 07:58:22 +0000
Subject: note 86273 added to function.array-push
Groups: php.notes 
Request: Send a blank email to php-notes+get-145241@lists.php.net to get a copy of this message
A function which mimics push() from perl, perl lets you push an array to an array: push(@array, @array2, @array3). This function mimics that behaviour. <?php function array_push_array(&$arr) { $args = func_get_args(); array_shift($args); if (!is_array($arr)) { trigger_error(sprintf("%s: Cannot perform push on something that isn't an array!", __FUNCTION__), E_USER_WARNING); return false; } foreach($args as $v) { if (is_array($v)) { if (count($v) > 0) { array_unshift($v, &$arr); call_user_func_array('array_push', $v); } } else { $arr[] = $v; } } return count($arr); } $arr = array(0); $arr2 = array(6,7,8); printf("%s\n", array_push_array($arr, array(),array(1,2,3,4,5), $arr2)); print_r($arr); # error.. $arr = "test"; printf("%s\n", array_push_array($arr, array(),array(1,2,3,4,5), $arr2)); ?> ---- Server IP: 89.248.170.61 Probable Submitter: 85.144.160.165 ---- Manual Page -- http://www.php.net/manual/en/function.array-push.php Edit -- https://master.php.net/note/edit/86273 Del: integrated -- https://master.php.net/note/delete/86273/integrated Del: useless -- https://master.php.net/note/delete/86273/useless Del: bad code -- https://master.php.net/note/delete/86273/bad+code Del: spam -- https://master.php.net/note/delete/86273/spam Del: non-english -- https://master.php.net/note/delete/86273/non-english Del: in docs -- https://master.php.net/note/delete/86273/in+docs Del: other reasons-- https://master.php.net/note/delete/86273 Reject -- https://master.php.net/note/reject/86273 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145241) next »