Home > Backend Development > PHP Tutorial > How Can I Merge Multiple Arrays into a Single Row-Based Structure in PHP?

How Can I Merge Multiple Arrays into a Single Row-Based Structure in PHP?

Barbara Streisand
Release: 2024-12-29 16:46:10
Original
262 people have browsed it

How Can I Merge Multiple Arrays into a Single Row-Based Structure in PHP?

Merging Data from Multiple Arrays for Row-Based Structures

In data manipulation tasks, it is often necessary to combine data from disparate sources to create a comprehensive dataset. This can be achieved by merging data from multiple arrays. Specifically, when the goal is to construct row-based structures, the array_merge_recursive function offers a powerful solution.

Consider the following example. Two arrays are given with the following structures:

  • Array 1:

    • ['gross_value' => '100', 'quantity' => '1']
    • ['gross_value' => '200', 'quantity' => '1']
  • Array 2:

    • ['item_title_id' => '1', 'order_id' => '4']
    • ['item_title_id' => '2', 'order_id' => '4']

The objective is to merge the contents of these arrays into a single array with the following structure:

  • ['gross_value' => '100', 'quantity' => '1', 'item_title_id' => '1', 'order_id' => '4']
  • ['gross_value' => '200', 'quantity' => '1', 'item_title_id' => '2', 'order_id' => '4']

To accomplish this, the array_merge_recursive function can be employed. This function recursively merges arrays, combining values with the same keys into nested arrays. However, in the given context, the arrays have numeric keys. For array_merge_recursive to work correctly, all numeric keys must be converted to strings (associative array).

The following code snippet demonstrates the solution:

$ar1 = [
    ['gross_value' => '100', 'quantity' => '1'],
    ['gross_value' => '200', 'quantity' => '1']
];

$ar2 = [
    ['item_title_id' => '1', 'order_id' => '4'],
    ['item_title_id' => '2', 'order_id' => '4']
];

// Convert numeric keys to strings
foreach ($ar1 as &$row) {
    $row = array_combine(array_keys($row), array_values($row));
}

foreach ($ar2 as &$row) {
    $row = array_combine(array_keys($row), array_values($row));
}

// Merge arrays
$result = array_merge_recursive($ar1, $ar2);

print_r($result);
Copy after login

Executing this code will produce the desired merged array, where the data from both arrays are combined into a single row-based structure.

The above is the detailed content of How Can I Merge Multiple Arrays into a Single Row-Based Structure 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template