PHP: Turning multidimensional arrays to single dimension arrays

前端 未结 7 607
遇见更好的自我
遇见更好的自我 2021-01-02 10:53

Basically my app is interacting with a web service that sends back a weird multidimensional array such as:

Array
(
    [0] => Array
        (
                     


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 11:18

    Consider $mArray as multidimensional array and $sArray as single dimensional array this code will ignore the parent array

    function flatten_array($mArray) {
        $sArray = array();
    
        foreach ($mArray as $row) {
            if ( !(is_array($row)) ) {
                if($sArray[] = $row){
                }
            } else {
                $sArray = array_merge($sArray,flatten_array($row));
            }
        }
        return $sArray;
    }
    

提交回复
热议问题