Hướng dẫn how do you add an element to an array at a given position in php? - Làm thế nào để bạn thêm một phần tử vào một mảng tại một vị trí nhất định trong php?

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

    $arr=[1,2,3,5];$arr=[1, 2,3,5];

    $pos=3;$pos =3;

    $val=4;$val=4;

    $result=array_merge(array_slice($arr,0,$pos),array($val),array_slice($arr,$pos));$result=array_merge(array_slice($arr, 0,$pos),array($val), array_slice($arr,$pos));

    print_r($result);print_r($result);

& 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ã

Đó 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

    $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=3;$pos=3;

    $val=array('y'=>'yellow');$val=array('y'=> 'yellow');

    $result=array_merge(array_slice($array,0,$pos),$val,array_slice($array,$pos));$result= array_merge(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; [2] => 3

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [3] => 4

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; [4] => 5

& nbsp; Cho các mảng kết hợp,

    )

    */

?>

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 :) :)


Làm thế nào để bạn thêm một phần tử vào một mảng ở một vị trí cụ thể?

Đây là cách làm điều đó ...
Đầu tiên lấy phần tử được chèn, nói phần tử ..
Sau đó, nhận được vị trí mà phần tử này sẽ được chèn, nói vị trí ..
Chuyển đổi Array thành ArrayList ..
Thêm phần tử tại vị trí bằng cách sử dụng danh sách.add (vị trí, phần tử).
Chuyển đổi ArrayList trở lại mảng và in ..

Làm thế nào để bạn thêm một mục vào một vị trí nhất định?

Cách tiếp cận: Để chèn một dữ liệu nhất định tại một vị trí được chỉ định, thuật toán dưới đây sẽ được tuân theo:..
Đi qua danh sách được liên kết lên đến các nút vị trí-1 ..
Khi tất cả các nút vị trí-1 được chuyển qua, phân bổ bộ nhớ và dữ liệu đã cho cho nút mới ..
Chỉ con trỏ tiếp theo của nút mới vào nút tiếp theo của nút hiện tại ..

Làm thế nào để bạn thêm một phần tử ở đầu một mảng trong PHP?

Sử dụng Array_unShift () để chèn phần tử đầu tiên vào một mảng.Sử dụng Array_Shift () để loại bỏ phần tử đầu tiên của một mảng.. Use array_shift() to remove the first element of an array.

Bạn có thể thêm vào một mảng trong PHP không?

Định nghĩa và cách sử dụng.Hàm mảng_push () chèn một hoặc nhiều phần tử vào cuối một mảng.Mẹo: Bạn có thể thêm một giá trị, hoặc bao nhiêu tùy thích.Lưu ý: Ngay cả khi mảng của bạn có các phím chuỗi, các phần tử được thêm vào của bạn sẽ luôn có các khóa số (xem ví dụ bên dưới).The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).