Home  >  Article  >  Backend Development  >  PHP framework - There are multiple arrays of the same level in the PHP array and I want to merge them into one array

PHP framework - There are multiple arrays of the same level in the PHP array and I want to merge them into one array

WBOY
WBOYOriginal
2016-12-01 00:25:321060browse

The printed result is multiple arrays of the same level. This is the first time I saw it and I want to merge it into one large array for easy operation

<code>
Array
(
    [0] => Array
        (
            [title] => 悦动行出游
        )

    [1] => Array
        (
            [title] => A游不错
        )

    [2] => Array
        (
            [title] => 游伴儿
        )

    [3] => Array
        (
            [title] => 房集市
        )

    [4] => Array
        (
            [title] => 大师讲解-达师解
        )

    [5] => Array
        (
            [title] => 带我飞
        )

    [6] => Array
        (
            [title] => 木西民宿
        )

    [7] => Array
        (
            [title] => 玩小伴宠物旅行
        )

    [8] => Array
        (
            [title] => 日本深度游学网
        )

    [9] => Array
        (
            [title] => 云游直播
        )

)
Array
(
    [0] => Array
        (
            [title] => 悠比旅行
        )

    [1] => Array
        (
            [title] => 快快旅行
        )

    [2] => Array
        (
            [title] => 独立日旅行
        )

    [3] => Array
        (
            [title] => BInkky创思瀚宇
        )

    [4] => Array
        (
            [title] => 鱿鱼旅行
        )

    [5] => Array
        (
            [title] => 有记YOKI
        )

    [6] => Array
        (
            [title] => 遇游邦
        )

    [7] => Array
        (
            [title] => 23place
        )

    [8] => Array
        (
            [title] => 同游会
        )

    [9] => Array
        (
            [title] => 哎哟旅行
        )

)</code>

Reply content:

The printed result is multiple arrays of the same level. This is the first time I saw it and I want to merge it into one large array for easy operation

<code>
Array
(
    [0] => Array
        (
            [title] => 悦动行出游
        )

    [1] => Array
        (
            [title] => A游不错
        )

    [2] => Array
        (
            [title] => 游伴儿
        )

    [3] => Array
        (
            [title] => 房集市
        )

    [4] => Array
        (
            [title] => 大师讲解-达师解
        )

    [5] => Array
        (
            [title] => 带我飞
        )

    [6] => Array
        (
            [title] => 木西民宿
        )

    [7] => Array
        (
            [title] => 玩小伴宠物旅行
        )

    [8] => Array
        (
            [title] => 日本深度游学网
        )

    [9] => Array
        (
            [title] => 云游直播
        )

)
Array
(
    [0] => Array
        (
            [title] => 悠比旅行
        )

    [1] => Array
        (
            [title] => 快快旅行
        )

    [2] => Array
        (
            [title] => 独立日旅行
        )

    [3] => Array
        (
            [title] => BInkky创思瀚宇
        )

    [4] => Array
        (
            [title] => 鱿鱼旅行
        )

    [5] => Array
        (
            [title] => 有记YOKI
        )

    [6] => Array
        (
            [title] => 遇游邦
        )

    [7] => Array
        (
            [title] => 23place
        )

    [8] => Array
        (
            [title] => 同游会
        )

    [9] => Array
        (
            [title] => 哎哟旅行
        )

)</code>

array_column, php version requires 5.5+

<code class="php">
$arr1 = Array
(
    Array
        (
            'title' => '悦动行出游',
        ),

     Array
        (
            'title' => 'A游不错',
        ));
  $arr2= Array
(
    Array
        (
            'title' => '悠比旅行',
        ),

     Array
        (
            'title' => '快快旅行',
        ));
        var_dump(array_merge(array_column($arr1,'title'),array_column($arr2,'title')));
    array(4) {
  [0]=>
  string(15) "悦动行出游"
  [1]=>
  string(10) "A游不错"
  [2]=>
  string(12) "悠比旅行"
  [3]=>
  string(12) "快快旅行"
}    
        </code>

Use array_merge method

<code>array_map(function($v){ 
    return $v['title'];
    }, $arr);</code>

Suppose your two two-dimensional arrays above are $arr1 and $arr2.
First use the function array_column() to obtain the column value of a two-dimensional array.
Get the value of the 'title' field in the two arrays into two anonymous index arrays respectively.
array_column($arr1, 'title');
Then use the array_merge() array merge function to merge the two arrays:
$new_arr = array_merge(array_column($arr1, 'title'), array_column($arr2, 'title' ));

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