Convert multidimensional array into single array

后端 未结 20 1807
时光取名叫无心
时光取名叫无心 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:23

    I have done this with OOP style

    $res=[1=>[2,3,7,8,19],3=>[4,12],2=>[5,9],5=>6,7=>[10,13],10=>[11,18],8=>[14,20],12=>15,6=>[16,17]];
    class MultiToSingle{
    public $result=[];
    public function __construct($array){
        if(!is_array($array)){
            echo "Give a array";
        }
        foreach($array as $key => $value){
            if(is_array($value)){
                for($i=0;$iresult[]=$value[$i];
                }  
            }else{
                $this->result[]=$value;
            }
        }
    }
    }
    
    $obj= new MultiToSingle($res);
    $array=$obj->result;
    print_r($array);
    

提交回复
热议问题