|
|
 |
array_unshift (PHP 4 , PHP 5) array_unshift --
Adiciona um ou mais elementos no início de um array
Descriçãoint array_unshift ( array array, mixed var [, mixed ...])
array_unshift() adiciona os elementos passados como
argumentos no início de array. Note que a lista
de elementos é adicionada como um todo, de forma que eles ficam na
mesma ordem. Todas as chaves numéricas serão modificadas para começar a
contar de 0 (zero) enquanto chaves strings permanecerão inalteradas.
Retorna o novo número de elementos de array.
Exemplo 1. Exemplo de array_unshift() |
<?php
$cesta = array("laranja", "banana");
array_unshift($cesta, "melancia", "morango");
?>
|
O programa acima resultaria em $queue contendo os
seguintes elementos:
|
Array
(
[0] => melancia
[1] => morango
[2] => laranja
[3] => banana
)
|
|
Veja também array_shift(),
array_push() e
array_pop().
php at electricsurfer dot com
27-Feb-2004 03:20
even simpler unshifting of a reference !
<?php
function array_unshift_ref(&$array, &$value)
{
$return = array_unshift($array,'');
$array[0] =& $value;
return $return;
}
?>
bitmore.co.kr
21-Feb-2004 01:17
//Original
//chris dot NoThxSpam dot given at hp dot com
//24-Jul-2003 01:17
//Note Infinite Loop Function
function array_key_change($Old, $New, $In, $NewVal=NULL) {
//if( !is_array($In) || !array_key_exists($Old,$In) ) return false;
//necessary Code
$Temp = array();
while(isset($Temp[$Old]) == false) {
list($k, $v) = each($In);
$Temp[$k] = $v;
unset($In[$k]);
}
if($NewVal == NULL) $NewVal = $Temp[$Old];
unset($Temp[$Old]);
$Temp = array_reverse($Temp);
$In = array_merge(array($New=>$NewVal), $In);
while(list($k,$v) = each($Temp)) $In = array_merge(array($k=>$v), $In);
return($In);
}
$In = array ("apple"=>1, "raspberry"=>2, "orange"=>3, "banana"=>4);
//¹«ÇÑÈ÷ ÁøÇàµÈ´Ù.OldŰ¿¡ ´ëÇÑ °ËÁõÇÊ¿ä
//infinite Funtion Process
//$Old = "test";
//$New = "bull";
//$Old = "";
//$New = "bull";
$Old = "apple";
$New = "bull";
//Array ( [bull] => 1 [raspberry] => 2 [orange] => 3 [banana] => 4 )
//$Old = "apple";
//$New = "";
//Array ( [] => 1 [raspberry] => 2 [orange] => 3 [banana] => 4 )
print_r( array_key_change($Old, $New, $In, $NewVal=NULL) );
lagroue
09-Nov-2003 01:46
Last version of PHP deprecated unshifting of a reference.
You can use this function instead :
<?php
function array_unshift1 (& $ioArray, $iValueWrappedInAnArray) {
$lNewArray = false;
foreach (array_keys ($ioArray) as $lKey)
$lNewArray[$lKey+1] = & $ioArray[$lKey];
$ioArray = array (& $iValueWrappedInAnArray[0]);
if ($lNewArray)
foreach (array_keys ($lNewArray) as $lKey)
$ioArray[] = & $lNewArray[$lKey];
return count($ioArray);
}
array_unshift ($a, &$v);
array_unshift1 ($a, array (&$v));
?>
lagroue
08-Nov-2003 06:32
Since deprecation of call-time pass-by-reference, you can not unshift with a reference :
<?php
array_unshift ($a, &$v);
?>
If you'd like to unshift only one reference, here is a replacement function :
<?php
function array_unshift1 (& $iArray, $iValueWrappedInAnArray) {
$lNewArray = false;
foreach (array_keys ($iArray) as $lKey)
$lNewArray[$lKey+1] = $iArray[$lKey];
$iArray = array (& $iValueWrappedInAnArray[0]);
foreach ($lNewArray as $lValue)
$iArray[] = $lValue;
return count($iArray);
}
?>
instead, you'll now call :
<?php
array_unshift1 ($a, array (&$v));
?>
for further implications of this rude deprecation, you can read http://masterofweb.com/index.php?itemid=102 (this is not my text)
chris dot NoThxSpam dot given at hp dot com
23-Jul-2003 09:17
If you need to change the name of a key without changing its position in the array this function may be useful.
<?php
function array_key_change($Old, $New, $In, $NewVal=NULL) {
$Temp = array();
while(isset($Temp[$Old]) == false) {
list($k, $v) = each($In);
$Temp[$k] = $v;
unset($In[$k]);
}
if($NewVal == NULL) {
$NewVal = $Temp[$Old];
}
unset($Temp[$Old]);
$Temp = array_reverse($Temp);
$In = array_merge(array($New=>$NewVal), $In);
while(list($k,$v) = each($Temp)) {
$In = array_merge(array($k=>$v), $In);
}
return($In);
}
?>
rsmith_NOSPAM_ at _NOSPAM_unitec dot ac dot nz
31-Jul-2002 04:00
array_merge() will also reindex (see array_merge() manual entry), but the '+' operator won't, so...
<?php
$arrayone=array("newkey"=>"newvalue") + $arrayone;
?>
does the job.
TimHyde at C21Technology dot com
18-Jul-2002 02:04
A simpler way to implement an array_unshift with key=>value pairs (i.e. similar to the example using array_reverse above) is to use array_merge. i.e.
<?php
$arrayone=array_merge(array("newkey"=>"newvalue"),$arrayone);
?>
Obviously you need to take care when adding numeric or duplicate keys.
jrh_at_geodata.soton.ac.uk
10-Jul-2002 06:30
I have found array_unshift is a function that should be avoided when unshifting lots of data in large arrays.
In a recent script I wrote, it took approx. 24 seconds to unshift 3500 timestamps to an array, a work around could be to use array_reverse and array_push. Array_push is much faster due to the indexing.
robert dot wills at fuzzbrain dot uklinux dot net
07-Feb-2002 03:02
Actually this problem with the keys getting reindexed only happens when the keys are numerical:
<?php
$a = array("f"=>"five", "s" =>"six", "t" =>
"twenty");
print_r($a);
echo "\n";
foreach($a as $key=>$val)
{
echo "k: $key v: $val \n";
}
array_unshift($a, "zero");
print_r($a);
echo "\n";
foreach($a as $key=>$val)
{
echo "k: $key v: $val \n";
}
?>
Array
(
[f] => five
[s] => six
[t] => twenty
)
k: f v: five
k: s v: six
k: t v: twenty
Array
(
[0] => zero
[f] => five
[s] => six
[t] => twenty
)
k: 0 v: zero
k: f v: five
k: s v: six
k: t v: twenty
sahn at hmc dot edu
27-Jul-2001 09:21
If you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:
<?php
function array_unshift_assoc(&$arr, $key, $val)
{
$arr = array_reverse($arr, true);
$arr[$key] = $val;
$arr = array_reverse($arr, true);
return count($arr);
}
?>
matt at synergie dot net
19-Sep-2000 07:20
The behaviour of unshift nearly caught me out.
Not only is the item added at the start of the list but the list is re-indexed too.
<?php
$a = array(5=>"five", 6 =>"six", 20 => "twenty");
while(list($key, $value) = each($a))
echo "k: $key v: $value<BR>\n";
echo "<BR>\n";
array_unshift($a, "zero");
while(list($key, $value) = each($a))
echo "k: $key v: $value<BR>\n";
?>
k: 5 v: five
k: 6 v: six
k: 20 v: twenty
k: 0 v: zero
k: 1 v: five
k: 2 v: six
k: 3 v: twenty
| |