Một hàm có thể chèn ở cả vị trí số nguyên và chuỗi:
/**
* @param array $array
* @param int|string $position
* @param mixed $insert
*/
function array_insert[&$array, $position, $insert]
{
if [is_int[$position]] {
array_splice[$array, $position, 0, $insert];
} else {
$pos = array_search[$position, array_keys[$array]];
$array = array_merge[
array_slice[$array, 0, $pos],
$insert,
array_slice[$array, $pos]
];
}
}
Sử dụng số nguyên:
$arr = ["one", "two", "three"];
array_insert[
$arr,
1,
"one-half"
];
// ->
array [
0 => 'one',
1 => 'one-half',
2 => 'two',
3 => 'three',
]
Sử dụng chuỗi:
$arr = [
"name" => [
"type" => "string",
"maxlength" => "30",
],
"email" => [
"type" => "email",
"maxlength" => "150",
],
];
array_insert[
$arr,
"email",
[
"phone" => [
"type" => "string",
"format" => "phone",
],
]
];
// ->
array [
'name' =>
array [
'type' => 'string',
'maxlength' => '30',
],
'phone' =>
array [
'type' => 'string',
'format' => 'phone',
],
'email' =>
array [
'type' => 'email',
'maxlength' => '150',
],
]
Bài đăng này sẽ thảo luận về cách chèn một mục tại một vị trí cụ thể trong một mảng trong PHP.
1. Sử dụng hàm array_slice[]
Một giải pháp đơn giản để chèn một mục tại một vị trí cụ thể trong một mảng là sử dụng hàm mảng_slice []. Ý tưởng là trích xuất một lát của mảng bằng hàm array_slice[]
và sau đó kết hợp lại các phần bằng hàm mảng_merge [].array_slice[] function. The idea is to extract a slice of the array using array_slice[]
function and then recombine the
parts using the array_merge[] function.
Mã sau đây cho thấy điều này:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Tải xuống & nbsp; & nbsp; mã
Đó là tất cả về việc chèn một vật phẩm ở một vị trí cụ thể trong một mảng trong PHP.
For associative arrays,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Tải xuống & nbsp; & nbsp; mã
Đó là tất cả về việc chèn một vật phẩm ở một vị trí cụ thể trong một mảng trong PHP.
For associative arrays, we can also use the union operator [+] to recombine the parts, which appends the
right-hand array to the left-hand array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; => 'màu đỏ', $array=array[$array=array[ 'b' =>'blue','b' =>'blue', & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [0] => 1'r' =>'red', & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [1] => 2'g' => 'green' ];]; $pos=1;$pos=1; $val=array['y'=>'yellow'];$val=array['y'=> 'yellow']; $result=array_slice[$array,0,$pos]+$val+array_slice[$array,$pos];$result= array_slice[$array,0,$pos]+ $val+array_slice[$array,$pos]; print_r[$result];print_r[$result]; & nbsp; & nbsp; & nbsp; & nbsp;/* output:/* Output: Array [ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [B] => & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbs & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbs & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [g] => ] */ ?> |
Tải xuống & nbsp; & nbsp; mã
& nbsp; Lưu ý rằng đối với một mảng bình thường trong đó các phím là số nguyên, toán tử +
có thể không hoạt động như mong đợi.
Note that for a normal array where the keys are integer, +
operator might not work as expected.
2. Sử dụng hàm array_splice[]
Một giải pháp khác là sử dụng hàm mảng_splice [], loại bỏ một phần của mảng và thay thế nó bằng các phần tử của mảng được chỉ định.array_splice[] function, which removes a portion of the array and replace it with the elements of the specified array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; => 'màu đỏ', $arr=[1,2,3,5];$arr=[1, 2,3,5]; $pos=3;$pos =3; $val=4;$val=4; array_splice[$arr,$pos,0,$val];array_splice[$arr,$pos,0, $val]; print_r[$arr];print_r[$arr]; & nbsp; & nbsp; & nbsp; & nbsp;/* output:/* Output: Array [ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [0] => 1 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [1] => 2 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [2] => 3 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [3] => 4 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [4] => 5 ] */ ?> |
Tải xuống & nbsp; & nbsp; mã
& nbsp; Lưu ý rằng đối với một mảng bình thường trong đó các phím là số nguyên, toán tử +
có thể không hoạt động như mong đợi.
Note that array_splice[]
does not preserve numeric keys. Consider the following example, which is trying to splice an associative array with numeric keys using array_splice[]
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; => 'màu đỏ', $arr=array[$arr=array[ 0=>'blue',0=>'blue', 1=>'red',1=>'red', 2=>'green'2=>'green' ];]; $pos=1;$pos=1; $val='yellow';$val ='yellow'; array_splice[$arr,$pos,0,$val];array_splice[$arr, $pos,0,$val]; print_r[$arr];print_r[$arr]; & nbsp; & nbsp; & nbsp; & nbsp;/* output:/* Output: Array [ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [0] => 1 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [1] => 2 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [2] => 3 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [3] => 4 ] */ ?> |
Tải xuống & nbsp; & nbsp; mã
& nbsp; Lưu ý rằng đối với một mảng bình thường trong đó các phím là số nguyên, toán tử +
có thể không hoạt động như mong đợi.
2. Sử dụng hàm array_splice[]
Một giải pháp khác là sử dụng hàm mảng_splice [], loại bỏ một phần của mảng và thay thế nó bằng các phần tử của mảng được chỉ định.
Như chúng tôi? Giới thiệu chúng tôi với bạn bè của bạn và giúp chúng tôi phát triển. Mã hóa hạnh phúc :] :]