2d multidimensional array to 1d array in php

后端 未结 8 1730
陌清茗
陌清茗 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条回答
  • 2021-01-06 01:36

    It depends on what you need but if you want to reduce your 2d array to a 1d curve that completley fills the 2d plane you probably looking for a spatial index or a space-filling-curve. There are some famous and not so known like the z-curve, the hilbert curve, the peano curve or the moore curve. You can write such a curve with a L-system.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题