2d multidimensional array to 1d array in php

后端 未结 8 1790
陌清茗
陌清茗 2021-01-06 00:51

Just wondering if anyone has transformed a 2 dim array to a one dim array in php. I\'ve yet to come across a clear explanation in php. Any suggestion would be appreciated.

8条回答
  •  萌比男神i
    2021-01-06 01:37

    Try this:

    function array_2d_to_1d ($input_array) {
        $output_array = array();
    
        for ($i = 0; $i < count($input_array); $i++) {
          for ($j = 0; $j < count($input_array[$i]); $j++) {
            $output_array[] = $input_array[$i][$j];
          }
        }
    
        return $output_array;
    }
    

提交回复
热议问题