Home  >  Article  >  Backend Development  >  How to insert array into array in php

How to insert array into array in php

PHPz
PHPzOriginal
2023-04-25 09:01:552152browse

PHP is a dynamic language that makes creating, reading and manipulating arrays very easy. Although a PHP array can contain various types of data, it can also contain other arrays. Such multidimensional arrays are called nested arrays. Nested arrays are implemented by making one array an element of another array. This article explains how to insert another array within a PHP array.

1. Use the array_merge() function

The array_merge() function merges two or more arrays into one array and returns a new array. You can use this function to merge two arrays into a new array.

For example:

$array1 = array("red","green");
$array2 = array("blue","yellow");
$new_array = array_merge($array1,$array2);
print_r($new_array);

This will output the following:

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => yellow
)

In this example, $array1 and $array2 contain two colors each. By using the array_merge() function, we merge these two arrays into a new array $new_array and output the result.

When using the array_merge() function, you must remember the following points:

  • This function can only be used for numbers or associative arrays. If there are identical string key names in the input array, this function overwrites the previous key value with the later key value.
  • The parameters of this function can be one or more arrays.

2. Use the " " operator

The " " operator in PHP can also merge arrays into one array. If two arrays have the same string key name, the later key value will overwrite the previous key value.

For example:

$array1 = array("red","green");
$array2 = array("blue","yellow");
$new_array = $array1 + $array2;
print_r($new_array);

This will output the following:

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => yellow
)

In this example, we use the " " operator to merge the two arrays into a new array $new_array , and output the result.

Different from the array_merge() function, when using the " " operator, if the key names are the same, the key value in the first array will be retained instead of overwriting it.

When using the " " operator, you must remember the following points:

  • This operator can be used for numbers or associative arrays.
  • It only adds the key value that does not exist in the first array to the new array (if even this key name already exists in the second array).

3. Nested arrays

PHP arrays can be used as elements of another array. Such multidimensional arrays are called nested arrays.

For example:

$fruits = array(
    "red" => array("apple"),
    "yellow" => array("banana", "lemon"),
    "green" => array("kiwi", "grape")
);

print_r($fruits);

This will output the following:

Array
(
    [red] => Array
        (
            [0] => apple
        )

    [yellow] => Array
        (
            [0] => banana
            [1] => lemon
        )

    [green] => Array
        (
            [0] => kiwi
            [1] => grape
        )

)

In this example we are using an associative array where each color is a reference to another array Key name. Each key name is associated with a nested array containing the names of fruits with that color.

To insert another array within an array, you can use the array_push() function, which adds one or more elements to the end of the array.

For example:

$fruits = array(
    "red" => array("apple"),
    "yellow" => array("banana", "lemon"),
    "green" => array("kiwi", "grape")
);

$fruits["red"][] = "strawberry";

print_r($fruits);

This will output the following:

Array
(
    [red] => Array
        (
            [0] => apple
            [1] => strawberry
        )

    [yellow] => Array
        (
            [0] => banana
            [1] => lemon
        )

    [green] => Array
        (
            [0] => kiwi
            [1] => grape
        )

)

In this example, we use the array_push() function to add a new element "strawberry" to The end of the subarray associated with "red" in the $fruits array.

When dealing with nested arrays, you must ensure that you are operating on the correct array. For example, to insert a new element into the description, you would use the following code:

$articles = array(
    "current" => array(
        "title" => "How to insert an array in PHP",
        "author" => "John Doe"
    ),
    "archive" => array(
        array(
            "title" => "10 useful PHP functions",
            "author" => "Jane Doe"
        ),
        array(
            "title" => "How to use loops in PHP",
            "author" => "John Doe"
        )
    )
);

$new_article = array(
    "title" => "How to create a multidimensional array",
    "author" => "Jane Doe"
);

array_push($articles["archive"], $new_article);

print_r($articles);

To add a new article to the "archive" subarray of the $articles array, we create a new array $new_article and use The array_push() function adds it to the end of the "archive" subarray.

In this example, we can also use []=$new_article to add a new array to the end of the "archive" subarray of $articles, and the result is the same.

Summary

Inserting an array into a PHP array can be achieved by using the array_merge() function, the " " operator and the array_push() function.

When adding an array, always make sure you are processing the correct subarray. In nested arrays, you can use a PHP array as an element of another array to create a multidimensional array.

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

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