PHP多维数组合并 [英] PHP multi- dimentional array merge

查看:70
本文介绍了PHP多维数组合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组:

$array['hello'][0][0]='a';
$array['hello'][0][1]='b';
$array['hello'][0][2]='c';
$array['hello'][0][3]='d';
$array['hello'][1][0]='e';
$array['hello'][1][1]='f';
$array['hello'][1][2]='g';
 .... // and so on

我想将它们合并并想要

$hello[1] = 'a';
$hello[2] = 'b';
$hello[3] = 'c';
$hello[4] = 'd';
$hello[5] = 'e';
$hello[6] = 'f';
$hello[7] = 'g';
 .... // and so on

现在我一直在使用 array_merge

 $hello = array_merge($array['hello'][0],$array['hello'][1]);

但是在此我必须具体说明密钥。 0 1

But in this i have to be specific regarding the keys ie. 0,1 . what if keys are not known.

还有其他方法吗?

推荐答案

这应该不需要键:

$hello = array();
foreach ($array['hello'] as $key => $value) {
    foreach ($value as $k => $v) {
            $hello[$k] = $v;
        }    
}

这篇关于PHP多维数组合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆