PHP按值索引的顺序合并多个数组 [英] PHP merge multiple arrays in order of their value index

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

问题描述

我发现了很多有关使用 array_merge 将数组连接在一起的信息,但是我想知道按值索引顺序合并多个数组而不是简单地将它们组合在一起有多么容易在一起.

I've found a lot of information on joining arrays together using array_merge, but I'm wondering how easy it is to merge multiple arrays in order of their value index, rather than simply joining them together.

例如,如果我们具有以下三个数组:

For example, if we had the following three arrays:

$a = array('One','Two','Three','Four');
$b = array(1,2,3,4);
$c = array('i','ii','iii','iv');

我们可以将它们合并吗?:

Could we merge them into?:

One,1,i,Two,2,ii,Three,3,iii,Four,4,iv

代替:

One, Two, Three, Four, 1, 2, 3, 4, i, ii, iii, iv

推荐答案

您可以这样编写自定义函数.

you can write your custom function like this.

$a = array('One','Two','Three','Four');
$b = array(1,2,3,4);
$c = array('i','ii','iii','iv');

$count = max(count($a), count($b), count($c));
$newarray = array();

for($i=0; $i < $count; $i++) {
   if (isset($a[$i])) $newarray[] = $a[$i];
   if (isset($b[$i])) $newarray[] = $b[$i];
   if (isset($c[$i])) $newarray[] = $c[$i];
}

var_dump($newarray);

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

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