What are the common ways to merge arrays in PHP?

PHPz
Release: 2024-04-28 13:36:01
Original
536 people have browsed it

In PHP, there are three common ways to merge arrays: using the array_merge() function, operator (for cases where key names do not conflict), and array_combine() function (for creating an array of key-value pairs). In practice, these methods can be used to merge the items in the shopping cart into the orders array.

What are the common ways to merge arrays in PHP?

Common ways of merging arrays in PHP

In PHP, it is often necessary to merge multiple arrays together to create a larger data collection. The following are several common methods to implement array merging:

array_merge() function

array_merge() The function will merge multiple arrays into one array and return the merged the array after. This function accepts a variable number of arrays as arguments. For example:

$array1 = ["foo", "bar"];
$array2 = ["baz", "qux"];

$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);

// 输出:
// Array
// (
//     [0] => foo
//     [1] => bar
//     [2] => baz
//     [3] => qux
// )
Copy after login

Operator

operator can also be used to merge arrays, but only if the key names of the two arrays do not conflict. For example:

$array1 = ["foo" => "bar"];
$array2 = ["baz" => "qux"];

$mergedArray = $array1 + $array2;

print_r($mergedArray);

// 输出:
// Array
// (
//     [foo] => bar
//     [baz] => qux
// )
Copy after login

array_combine() function

array_combine() The function can combine two arrays into an array of key-value pairs. The values ​​of the first array serve as keys and the values ​​of the second array serve as values. For example:

$keys = ["foo", "bar"];
$values = ["baz", "qux"];

$mergedArray = array_combine($keys, $values);

print_r($mergedArray);

// 输出:
// Array
// (
//     [foo] => baz
//     [bar] => qux
// )
Copy after login

Practical case: Merge the items in the shopping cart into the order array

Consider the shopping cart function of an e-commerce website, where each item is stored in an array. When a user places an order, we need to merge all the items in the shopping cart into an order array.

$cartItems = [
    ["product_id" => 1, "quantity" => 2],
    ["product_id" => 2, "quantity" => 1],
];

$order = [
    "customer_id" => 123,
    "order_date" => "2022-08-24",
];

// 使用 array_merge() 函数合并数组
$mergedArray = array_merge($order, $cartItems);

// 将合并后的数组插入数据库以创建订单
// ...
Copy after login

The above is the detailed content of What are the common ways to merge arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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