简体   繁体   English

合并每个其他数组PHP

[英]Merge every other array php

array one: 1,3,5,7 array two: 2,4,6,8 阵列一: 1,3,5,7阵列二: 2,4,6,8

the array i want would be 1,2,3,4,5,6,7,8 我想要的数组是1,2,3,4,5,6,7,8

I'm just using numbers as examples. 我只是用数字作为例子。 If it was just numbers i could merge and sort but they will be words. 如果它只是数字,我可以合并和排序,但他们将是文字。 So maybe something like 也许是类似的东西

array one: bob,a,awesome 阵列一: bob,a,awesome

array two: is,really,dude 阵列二: is,really,dude

should read: bob is a really awesome dude 应该读: bob is a really awesome dude

Not really sure how to do this. 不确定如何做到这一点。 Does PHP have something like this built in? PHP内置了这样的东西吗?

You could write yourself a function like this: 你可以自己写一个这样的函数:

function array_merge_alternating($array1, $array2) {
    if(count($array1) != count($array2)) {
        return false; // Arrays must be the same length
    }

    $mergedArray = array();

    while(count($array1) > 0) {
        $mergedArray[] = array_shift($array1);
        $mergedArray[] = array_shift($array2);
    }
    return $mergedArray;
}

This function expects two arrays with equal length and merges their values. 此函数需要两个长度相等的数组并合并它们的值。

If you don't need your values in alternating order you can use array_merge . 如果您不需要交替顺序的值,则可以使用array_merge array_merge will append the second array to the first and will not do what you ask. array_merge会将第二个数组追加到第一个数组,并且不会按照你的要求执行。

Try this elegant solution 试试这个优雅的解决方

function array_alternate($array1, $array2)
{
    $result = Array();
    array_map(function($item1, $item2) use (&$result)
                {
                    $result[] = $item1;
                    $result[] = $item2;             
                }, $array1, $array2);
    return $result;
}

This solution works AND it doesn't matter if both arrays are different sizes/lengths: 此解决方案有效,并且两个阵列的大小/长度不同并不重要:

function array_merge_alternating($array1, $array2)
{
  $mergedArray = array();

  while( count($array1) > 0 || count($array2) > 0 )
  {
    if ( count($array1) > 0 )
      $mergedArray[] = array_shift($array1);
    if ( count($array2) > 0 )
      $mergedArray[] = array_shift($array2);
  }
  return $mergedArray;
}

Try this function: 试试这个功能:

function arrayMergeX()
{
  $arrays = func_get_args();
  $arrayCount = count($arrays);

  if ( $arrayCount < 0 )
    throw new ErrorException('No arguments passed!');

  $resArr = array();

  $maxLength = count($arrays[0]);

  for ( $i=0; $i<$maxLength; $i+=($arrayCount-1) )
  {
    for ($j=0; $j<$arrayCount; $j++)
    {
      $resArr[] = $arrays[$j][$i];
    }
  }
  return $resArr;
}

var_dump( arrayMergeX(array(1,3,5,7), array(2,4,6,8)) );
var_dump( arrayMergeX(array('You', 'very'), array('are', 'intelligent.')) );
var_dump( arrayMergeX() );

It works with variable numbers of arrays! 它适用于可变数量的数组!

Live on codepad.org: http://codepad.org/c6ZuldEO 住在codepad.org: http://codepad.org/c6ZuldEO

if arrays contains numeric values only, you can use merge and sort the array. 如果数组仅包含数值,则可以使用合并并对数组进行排序。

<?php
    $a = array(1,3,5,7);
    $b = array(2,4,6,8);

    $merged_array = array_merge($a,$b);
    sort($merged,SORT_ASC);
?>

else use this solution. 否则使用此解决方案。

<?php
    function my_merge($array1,$array2)
    {
        $newarray = array();
        foreach($array1 as $key => $val)
        {
            $newarray[] = $val;
            if(count($array2) > 0)
                $newarray[] = array_shift($array2)
        } 

        return $newarray;
    }

?>

hope this help 希望这个帮助

Expects both arrays to have the same length: 期望两个数组具有相同的长度:

$result = array();

foreach ($array1 as $i => $elem) {
   array_push($result, $elem, $array2[$i]);
}

echo join(' ', $result);

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

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