简体   繁体   English

合并2个数组交换索引PHP

[英]Merge 2 Arrays Interchanging Index PHP

Not sure if I described it properly in the title, but I'm looking to merge 2 arrays in php, although I don't want the new array to have the first array first and then the second array at the end, instead I want it to have the first item of the first array, first item of the second array, 2nd item of the first array, etc 我不确定我是否在标题中正确描述了它,但是我想在php中合并2个数组,虽然我不希望新数组首先拥有第一个数组,然后在结束时使用第二个数组,而不是我想要的它拥有第一个数组的第一项,第二个数组的第一项,第一个数组的第二项等

so essentially 基本上

Array 1
     A - 1
     A - 2
     A - 3

Array 2
     B - 1
     B - 2
     B - 3

and after merging the two, the resulting array will be 并且在合并两者之后,得到的数组将是

Array Merged
     A - 1
     B - 1
     A - 2
     B - 2
     A - 3
     B - 3

In that order, can anyone help me out? 按此顺序,任何人都可以帮助我吗?

Merge and sort them, while keeping the keys 在保持键的同时合并和排序它们

$array1 = array(/* values here */);
$array2 = array(/* values here */);

$mergedArray = array_merge($array1, $array2);
ksort($mergedArray); // this also keeps the keys

Note: 注意:

Your arrays can't work, since you can't have 2 entries with the same keys :) 您的阵列无法工作,因为您不能拥有2个具有相同键的条目:)

Basically $array2 = array('B' => 1, 'B' => 2, 'B' => 3); 基本上$array2 = array('B' => 1, 'B' => 2, 'B' => 3); will result in the following array: 将导致以下数组:

Array
(
    [B] => 3
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM