$v){...}"; 2. Convert by looping through the three-dimensional array."/> $v){...}"; 2. Convert by looping through the three-dimensional array.">

Home >Backend Development >PHP Problem >How to convert php three-dimensional array to two-dimensional array

How to convert php three-dimensional array to two-dimensional array

藏色散人
藏色散人Original
2021-06-01 09:43:114030browse

php method to convert a three-dimensional array into a two-dimensional array: 1. Convert through the "foreach ($result as $key =>$v){...}" method; 2. Traverse the three-dimensional array through a loop array to convert.

How to convert php three-dimensional array to two-dimensional array

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php three-dimensional array into two-dimensional array

<?php
    $result = Array(0 => Array(0 => Array(bid => 41,brealname => &#39;we教官&#39;,cid => 41,crealname => &#39;we教官&#39;)),
                    1 => Array(0 => Array(bid => 6,brealname => &#39;虎子&#39;,cid => 19,crealname => &#39;张鱼&#39;))
    );
    //原数组
    print_r($result);
    echo "<br />";
    //第一种方法:
    foreach ($result as $key =>$v){
        $new_arr[]=$v[0];
    }
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($new_arr);
    //第二种方法
    //初始化$arr2  
    $arr2=array();  
           
    //循环遍历三维数组$arr3  
    foreach($result as $value){  
        foreach($value as $v){  
             $arr2[]=$v;  
        }  
    }
    
    //销毁$arr3 
    unset($result,$value,$v);
    echo "<br /s>";
    echo "第二种方法:";
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($arr2);
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php three-dimensional array to two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn