Hàm array_unshift[]
sẽ thêm một hoặc nhiều phần tử vào đầu mảng.Hàm trả về số nguyên là số phần tử của mảng mới được thêm.
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Cú pháp
Cú pháp: array_unshift[$array, $element1, $element2, ....];
Trong đó:
$array
là mảng dữ liệu ban đầu.$element1, $element2, ....
là các phần tử được thêm vào.
Ví dụ
Code
$array = array[ "php", "js" ]; array_unshift[$array, "html", "css"]; echo ""; print_r[$array]; echo "";
Kết quả
Array [ [0] => html [1] => css [2] => php [3] => js ]
Hai phần tử html
và css
đã được thêm vào đầu mảng $array
.
Bài viết này được đăng tại [free tuts .net]
Tham khảo: php.net
Định nghĩa hàm array_unshift[]
trong PHP
__Hàm array_unshift[]
trong PHP__ thêm một hoặc nhiều phần tử tới phần đầu của mảng
Cú pháp của hàm array_unshift[]
trong PHP là:
array_unshift[$array,$value1,$value2,$value3...]
Tham số
- array: Bắt buộc. Xác định một mảng
- value1: Bắt buộc. Xác định một value để chèn
- value2: Tùy ý. Xác định một value để chèn
- value3: Tùy ý. Xác định một value để chèn
Ví dụ minh họa cách sử dụng hàm array_unshift[]
trong PHP:
Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ //localhost:8080/test.php sẽ cho kết quả:
Xem thêm Hàm trong php
Hàm array_unshift[]
sẽ thêm một hoặc nhiều phần tử vào đầu mảng.Hàm trả về số nguyên là số phần tử của mảng mới được thêm.
Nội dung chính
- Định nghĩa hàm array_unshift[] trong PHP
- Cú pháp của hàm array_unshift[] trong PHP là:
- Ví dụ minh họa cách sử dụng hàm array_unshift[] trong PHP:
- Description
- Return Values
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Cú pháp
Cú pháp: array_unshift[$array, $element1, $element2, ....];
Trong đó:
$array
là mảng dữ liệu ban đầu.$element1, $element2, ....
là các phần tử được thêm vào.
Ví dụ
Code
$array = array[ "php", "js" ]; array_unshift[$array, "html", "css"]; echo ""; print_r[$array]; echo "";
Kết quả
Array [ [0] => html [1] => css [2] => php [3] => js ]
Hai phần tử html
và css
đã được thêm vào đầu mảng $array
.
Bài viết này được đăng tại [free tuts .net]
Tham khảo: php.net
Định nghĩa hàm array_unshift[] trong PHP
__Hàm array_unshift[]
trong PHP__ thêm một hoặc nhiều phần tử tới phần đầu của mảng
Cú pháp của hàm array_unshift[] trong PHP là:
array_unshift[$array,$value1,$value2,$value3...]
Tham số
- array: Bắt buộc. Xác định một mảng
- value1: Bắt buộc. Xác định một value để chèn
- value2: Tùy ý. Xác định một value để chèn
- value3: Tùy ý. Xác định một value để chèn
Ví dụ minh họa cách sử dụng hàm array_unshift[] trong PHP:
Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ //localhost:8080/test.php sẽ cho kết quả:
Xem thêm Hàm trong php
Hàm
array_unshift[]
sẽ thêm một hoặc nhiều phần tử vào đầu mảng.Hàm trả về số nguyên là số phần tử của mảng mới được thêm.
Cú pháp
Cú pháp: array_unshift[$array, $element1, $element2, ....];
Trong đó:
$array
là mảng dữ liệu ban đầu.$element1, $element2, ....
là các phần tử được thêm vào.
Ví dụ
Code
$array = array[ "php", "js" ]; array_unshift[$array, "html", "css"]; echo ""; print_r[$array]; echo "";
Kết quả
Array [ [0] => html [1] => css [2] => php [3] => js ]
Hai phần tử html
và css
đã được thêm
vào đầu mảng $array
.
Tham khảo: php.net
[PHP 4, PHP 5, PHP 7, PHP 8]
array_unshift — Prepend one or more elements to the beginning of an array
Description
array_unshift[array &$array
, mixed ...$values
]: int
Note:
Resets array's internal pointer to the first element.
Parameters
array
The input array.
values
The values to prepend.
Return Values
Returns the new number of elements in the array
.
Changelog
7.3.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Examples
Example #1 array_unshift[] example
The above example will output:
Array [ [0] => apple [1] => raspberry [2] => orange [3] => banana ]
See Also
- array_shift[] - Shift an element off the beginning of array
- array_push[] - Push one or more elements onto the end of array
- array_pop[] - Pop the element off the end of array
sergei at gmx dot net ¶
14 years ago
You can preserve keys and unshift an array with numerical indexes in a really simple way if you'll do the following:
now the array looks as follows:
array[
100=>'Test Element 1 ',
255=>'Test Element 2'
224=>'someword1',
228=>'someword2',
102=>'someword3',
544=>'someword3',
95=>'someword4'
];
Anonymous ¶
10 years ago
Sahn's example almost works but has a small error. Try it like this 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:
daniel at smallboxcms dot com ¶
7 years ago
Anonymous' associative version wasn't working for me, but it did with this small tweak:
function array_unshift_assoc[&$arr, $key, $val]
{
$arr = array_reverse[$arr, true];
$arr[$key] = $val;
$arr = array_reverse[$arr, true];
return $arr;
}
Richard Akindele ¶
6 years ago
Another way to tack something to the beginning of an array is with array_merge[].
$plans = array['AARP'=>'Senior', 'AAA'=>'Automobile Club'];
$plans = array_merge[array["BAR"=>"Best Available Rate"], $plans];
amschroeder at gmail dot com ¶
15 years ago
This becomes a nice little problem if you index your arrays out of order [while manually sorting]. For example:
singleMonth: 3/%/2007
It reindexes them based on the order they were created. It seems like if an array has all numeric indexes, then it should reindex them based on the order of their index. Just my opinion...
Produces:
singleMonth: 2/%/2007
singleMonth: 1/%/2007
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: %
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: 1/%/2007
php at electricsurfer dot com ¶
18 years ago
even simpler unshifting of a reference !
robert dot wills at fuzzbrain dot uklinux dot net ¶
20 years ago
Actually this problem with the keys getting reindexed only happens when the keys are numerical:
k: f v: five k: 0 v: zero
Array
[
[f] => five
[s] => six
[t] => twenty
]
k: s v: six
k: t v: twenty
Array
[
[0] => zero
[f] => five
[s] => six
[t] => twenty
]
k: f v: five
k: s v: six
k: t v: twenty
sahn at hmc dot edu ¶
21 years ago
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:
chris dot NoThxSpam dot given at hp dot com ¶
19 years ago
If you need to change the name of a key without changing its position in the array this function may be useful.
John Brooking ¶
16 years ago
I had a need tonight to convert a numeric array from 1-based to 0-based, and found that the following worked just fine due to the "side effect" of renumbering:
lagroue ¶
18 years ago
Last version of PHP deprecated unshifting of a reference.
You can use this function instead :