How to add elements to multidimensional array in php

不言
Release: 2023-04-04 18:56:01
Original
8034 people have browsed it

Multidimensional array refers to an array containing array elements, so how to add elements to a multidimensional array? This article will introduce to you how to add elements to multi-dimensional arrays in PHP.

How to add elements to multidimensional array in php

Multidimensional arrays are written as follows:

$multi_dimensional_array = [
    ["张三", "25", "male"],
    ["李四", "18", "female"],
    ["王二", "29", "female"]
];
Copy after login

In this way, you can store and manage multiple arrays in an array.

Add elements to a multidimensional array

Add elements to the end of the array

To add elements to To add to the end of a multidimensional array, use [] to add elements or use the array_push function to add elements.

Let’s look at specific examples

$multi_dimensional_array = [
    ["张三", "25", "male"],
    ["李四", "18", "female"],
    ["王二", "29", "female"]
];
$multi_dimensional_array[] = ["陈五", "20", "male"];
var_dump($multi_dimensional_array);
Copy after login

The output results of the above example code are as follows.

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "张三"
    [1]=>
    string(2) "25"
    [2]=>
    string(4) "male"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "李四"
    [1]=>
    string(2) "18"
    [2]=>
    string(6) "female"
  }
  [2]=>
  array(3) {
    [0]=>
    string(6) "王二"
    [1]=>
    string(2) "29"
    [2]=>
    string(6) "female"
  }
  [3]=>
  array(3) {
    [0]=>
    string(6) "陈五"
    [1]=>
    string(2) "20"
    [2]=>
    string(4) "male"
  }
}
Copy after login

The added element is stored at the end of the array

Another way to add an element to the end of the array is to call the array_push

array_push function to specify the second The elements of the parameter, the array to be added to the first parameter.

Let’s try using the array_push function, as shown in the sample code below.

$multi_dimensional_array = [
    ["张三", "25", "male"],
    ["李四", "18", "female"],
    ["王二", "29", "female"]
];
array_push($multi_dimensional_array,["陈五", "20", "male"]);
var_dump($multi_dimensional_array);
Copy after login

The output will be the same as above.

Add elements to the beginning of the array

To add elements to the beginning of the array we need to use the array_unshift function

The code is as follows

$multi_dimensional_array = [
    ["张三", "25", "male"],
    ["李四", "18", "female"],
    ["王二", "29", "female"]
];
array_unshift($multi_dimensional_array,["陈五", "20", "male"]);
var_dump($multi_dimensional_array);
Copy after login

The output result is as follows

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "陈五"
    [1]=>
    string(2) "20"
    [2]=>
    string(4) "male"
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "张三"
    [1]=>
    string(2) "25"
    [2]=>
    string(4) "male"
  }
  [2]=>
  array(3) {
    [0]=>
    string(6) "李四"
    [1]=>
    string(2) "18"
    [2]=>
    string(6) "female"
  }
  [3]=>
  array(3) {
    [0]=>
    string(6) "王二"
    [1]=>
    string(2) "29"
    [2]=>
    string(6) "female"
  }
}
Copy after login

It can be seen from the output result that the elements added by the array_unshift function are added to the beginning.

This article ends here. For more exciting content, you can pay attention to other related column tutorials on the How to add elements to multidimensional array in php Chinese website! ! !

The above is the detailed content of How to add elements to multidimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template