Home > Backend Development > PHP Problem > How to convert two-dimensional array to one-dimensional array in php

How to convert two-dimensional array to one-dimensional array in php

青灯夜游
Release: 2023-03-14 10:10:02
Original
13586 people have browsed it

php Method to convert a two-dimensional array to a one-dimensional array: 1. Use the array_column() function; 2. Use the array_walk() function; 3. Use the array_map() function; 4. Use the array_reduce() function; 5 , use array_walk_recursive() function.

How to convert two-dimensional array to one-dimensional array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Summary of converting two-dimensional array to one-dimensional array

For example, convert the following two-digit array into a one-dimensional array

$records = [
    [
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ],
    [
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ],
    [
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ],
    [
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    ]
];
Copy after login

1.array_column()

array_column() is PHP built-in functions, the restriction is that the PHP version must be 5.5.0 and above!

Example 1:

<?php
$first_names = array_column($records, &#39;first_name&#39;);
var_dump($first_names);
?>
Copy after login

The print result is:

How to convert two-dimensional array to one-dimensional array in php

##Example 2:

<?php
$first_names = array_column($records, &#39;first_name&#39;,&#39;id&#39;);
var_dump($first_names);
?>
Copy after login

The print result is:

How to convert two-dimensional array to one-dimensional array in php

2.array_walk()

array_walk() function uses a user-defined function to Callback processing is performed on each element to implement the current function:

$first_names= [];
array_walk($records, function($value, $key) use (&$first_names){
    $first_names[] = $value[&#39;first_name&#39;];
});
var_dump($first_names);
Copy after login

The printed result is:

How to convert two-dimensional array to one-dimensional array in php

3.array_map()

The array_map() function is similar to array_walk(), applying the callback function to the cells of the given array.

$first_names= [];
array_map(function($value) use (&$first_names){
    $first_names[] = $value[&#39;first_name&#39;];
}, $records);
var_dump($first_names);
Copy after login

The printed result is:

How to convert two-dimensional array to one-dimensional array in php

4.array_reduce()

array_reduce — Use a callback function to iteratively reduce an array to a single value.

$first_names = array_reduce($records,function($result, $value){
    array_push($result, $value[&#39;first_name&#39;]);
    return $result;
},[]);
var_dump($first_names);
Copy after login
Print results:

How to convert two-dimensional array to one-dimensional array in php

5. array_walk_recursive()

array_walk_recursive — Applies the user function recursively to each member of the array. This function can convert an array of any dimension into a one-dimensional array.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to convert two-dimensional array to one-dimensional array 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