Home >Backend Development >PHP Tutorial > PHP 将二维数组转成1维数组 键名保持不变 并且去掉空元素

PHP 将二维数组转成1维数组 键名保持不变 并且去掉空元素

WBOY
WBOYOriginal
2016-06-13 13:24:251061browse

PHP 将二维数组转成一维数组 键名保持不变 并且去掉空元素

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
array(
    0=>array(
        'zhangsan' => 'zhangsan (dae) abc'
    )

    1=>array(
        'lisi' => 'lisi (dae) abc'
    )
       
        2=>array()
)

转成
array(
    'zhangsan' => 'zhangsan (dae) abc',
    'lisi' => 'lisi (dae) abc',
)




谢谢了

------解决方案--------------------
PHP code
$a = array(
    0=>array(
        'zhangsan' => 'zhangsan (dae) abc'
    ),
    1=>array(
        'lisi' => 'lisi (dae) abc'
    ),
    2=>array()
);

$list = array();
foreach ($a as $v) {
    if (!$v) {
        continue;
    }
    $list[key($v)] = $v[key($v)];
} <div class="clear">
                 
              
              
        
            </div>
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