How to combine array of array values into one array?

人盡茶涼 提交于 2019-11-30 19:30:01

问题


I have following array,

Array
(
    [14289] => Array
        (
            [0] => Ability||STROKE CLINIC,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child1||FC
            [1] => Ability||N/S++,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child2||SC
            [2] => Ability||B-,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child3||TC
        )

    [14279] => Array
        (
            [0] => Ability||STROKE CLINIC,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child1||FC
            [1] => Ability||N/S++,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child2||SC
            [2] => Ability||B-,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child3||TC
        )

    [14284] => Array
        (
            [0] => Ability||STROKE CLINIC,Session||Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time||#1 only: 1:30pm,child1||FC
            [1] => Ability||N/S++,Session||Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time||#1 only: 1:30pm,child2||SC
        )

)

i need this array as below,

Array
(


            [0] => Ability||STROKE CLINIC,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child1||FC
            [1] => Ability||N/S++,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child2||SC
            [2] => Ability||B-,Session||Session #3: Tues June 28th - Fri July 8th (9-2:00PM),Time||#1 only: 2pm,child3||TC
            [3] => Ability||STROKE CLINIC,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child1||FC
            [4] => Ability||N/S++,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child2||SC
            [5] => Ability||B-,Session||Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time||#1 only: 1pm,child3||TC          
            [6] => Ability||STROKE CLINIC,Session||Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time||#1 only: 1:30pm,child1||FC
            [7] => Ability||N/S++,Session||Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time||#1 only: 1:30pm,child2||SC


)

How can i do this?


回答1:


$result = array();
foreach($array as $item) {
    $result = array_merge($result, $item);
}



回答2:


You're looking for array_merge to merge the (sub-) arrays. This can be called via call_user_func_array for an easy interface:

$result = call_user_func_array('array_merge', $array);

See as well:

  • PHP array merge from unknown number of parameters
  • Convert multidimensional array into single array



回答3:


$new_arr = array();
array_walk_recursive($arr, function($item) use(&$new_arr)
{
    $new_arr[] = $item;
});



回答4:


Considering your array is :

$z = array(
               '14289' =>
               array('a',
                     'b',
                     'c'
                    ),
               '14290' =>
               array('d',
                     'e',
                     'f'
                    ),
               '14291' =>
               array('g',
                     'h',
                     'i'
                    )
              );

then,

$y =array();// use a blank array to get your result
array_map(function($a) use(&$y){ $y = array_merge( $y,array_values($a)); },$z);

var_dump($y);


来源:https://stackoverflow.com/questions/9413583/how-to-combine-array-of-array-values-into-one-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!