Home > Backend Development > PHP Tutorial > How to consider data type compatibility when merging PHP arrays?

How to consider data type compatibility when merging PHP arrays?

PHPz
Release: 2024-04-28 16:54:01
Original
795 people have browsed it

PHP When merging arrays, data type compatibility is crucial. Different merging methods handle it differently: array_merge(): append elements and convert them to strings; array_combine(): match keys and values. If the keys are insufficient, leave them blank. ; = operator: merge numeric key arrays and add key values ​​with the same name.

How to consider data type compatibility when merging PHP arrays?

How to consider data type compatibility in PHP array merging

When merging arrays in PHP, consider data type compatibility Crucial, as this affects the contents and type of the merged array. PHP provides a variety of array merging methods, each with its own way of handling data types.

1. array_merge()

array_merge() method simply appends all elements of the input array together. It converts elements of any type to string regardless of data type.

$array1 = [1, 'foo', true];
$array2 = ['bar', 2.5, null];

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

print_r($mergedArray);
Copy after login

Output:

Array
(
    [0] => 1
    [1] => foo
    [2] => true
    [3] => bar
    [4] => 2.5
    [5] => null
)
Copy after login

2. array_combine()

array_combine() method combines the corresponding elements of the two arrays Pairing creates an associative array. If an element is missing from the key array, it will leave the associated value empty.

$keys = ['a', 'b', 'c'];
$values = [1, 'foo', true];

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

print_r($combinedArray);
Copy after login

Output:

Array
(
    [a] => 1
    [b] => foo
    [c] => true
)
Copy after login

3. = operator

The = operator can merge arrays, but it only applies to numeric key arrays. It adds elements with the same key.

$array1 = ['foo' => 1, 'bar' => 2];
$array2 = ['foo' => 3, 'baz' => 4];

$array1 += $array2;

print_r($array1);
Copy after login

Output:

Array
(
    [foo] => 4
    [bar] => 2
)
Copy after login

Practical case

Consider the following scenario:

  • There is a user array containing integers ID and username.
  • There is an order array containing string order ID and order total.

These two arrays need to be combined to give each user their total order amount.

$users = [
    1 => 'Alice',
    2 => 'Bob',
    3 => 'Charlie'
];

$orders = [
    'order-1' => 100,
    'order-2' => 200,
    'order-3' => 300
];

// 将用户 ID 转换为字符串以匹配订单键
$userIDs = array_keys($users);
$strUserIDs = array_map('strval', $userIDs);

// 使用 array_combine() 将用户 ID 与总计相匹配
$userTotals = array_combine($strUserIDs, array_fill(0, count($userIDs), 0));

// 循环用户数组并更新总计
foreach ($orders as $orderID => $total) {
    $userID = $orderID[0];
    $userTotals[$userID] += $total;
}

print_r($userTotals);
Copy after login

Output:

Array
(
    [1] => 100
    [2] => 200
    [3] => 300
)
Copy after login

By considering data type compatibility, we were able to successfully merge the two arrays and extract the required data.

The above is the detailed content of How to consider data type compatibility when merging PHP arrays?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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