Convert multidimensional array into single array

后端 未结 20 1569
时光取名叫无心
时光取名叫无心 2020-11-22 10:39

I have an array which is multidimensional for no reason

/* This is how my array is currently */
Array
(
[0] => Array
    (
        [0] => Array
                


        
20条回答
  •  长情又很酷
    2020-11-22 11:12

    Recently I've been using AlienWebguy's array_flatten function but it gave me a problem that was very hard to find the cause of.
    array_merge causes problems, and this isn't the first time that I've made problems with it either.
    If you have the same array keys in one inner array that you do in another, then the later values will overwrite the previous ones in the merged array.

    Here's a different version of array_flatten without using array_merge:

    function array_flatten($array) { 
      if (!is_array($array)) { 
        return FALSE; 
      } 
      $result = array(); 
      foreach ($array as $key => $value) { 
        if (is_array($value)) { 
          $arrayList=array_flatten($value);
          foreach ($arrayList as $listItem) {
            $result[] = $listItem; 
          }
        } 
       else { 
        $result[$key] = $value; 
       } 
      } 
      return $result; 
    } 
    

提交回复
热议问题