开发者

Combine arrays in PHP [duplicate]

This question already has answers here: Closed 11 years ago.

Possible开发者_运维知识库 Duplicate:

Transposing multidimensional arrays in PHP

I have three arrays:

$a = (a1, a2, a3);
$b = (b1, b2, b3);
$c = (c1, c2, c3);

I want to combine these arrays to create:

$newa = (a1, b1, c1);
$newb = (a2, b2, c2);
$newc = (a3, b3, c3);


function transpose(){
    $array = func_get_args();
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}

$a = array('a1', 'a2', 'a3');
$b = array('b1', 'b2', 'b3');
$c = array('c1', 'c2', 'c3');

list($newa, $newb, $newc) = transpose($a,$b,$c);

var_dump($newa, $newb, $newc);

Transpose function from here, and slightly modified by me.


I changed things a little. I didn't use $newA, #newB, $newC, but used a $m array instead.

    $a = array( "a1", "a2", "a3");
    $b = array( "b1", "b2", "b3");
    $c = array( "c1", "c2", "c3");

$count = count($a);
$i = 0;
$m = array();
for($i;$i < $count; $i++) {
        $m[] = array( $a[$i], $b[$i], $c[$i]);
}

print_r($m);

Outputs:

Array
(
    [0] => Array
        (
            [0] => a1
            [1] => b1
            [2] => c1
        )

    [1] => Array
        (
            [0] => a2
            [1] => b2
            [2] => c2
        )

    [2] => Array
        (
            [0] => a3
            [1] => b3
            [2] => c3
        )

)


How about this...

<?php

$arr1 = array(1, 2, 3);
$arr2 = array(1, 2, 3);
$arr3 = array(1, 2, 3);

function array_divide() {
    $ret = array();
    foreach (func_get_args() as $array) {
        foreach ($array as $key=>$value) {
            $ret[$key][] = $value;
        }
    }
    return $ret;
}

list($new1, $new2, $new3) = array_divide($arr1, $arr2, $arr3);
var_dump($new1);

Obviously the function can only return one value, so you'll need list if you want to get multiple values out of it.


Here's my bid. Tried to make it as flexible as possible:

function array_rotate(){
  $args = func_get_args();
  $argc = func_num_args();

  // check if all items are arrays and also get the
  // length of the longest array
  $maxCount = 0;
  foreach ($args as $arg){
    if (!is_array($arg))
      throw new ArgumentException("array_rotate: All arguments must be arrays");
    $_count = count($arg);
    if ($_count > $maxCount)
      $maxCount = $_count;
  }

  // setup the returned result
  $result = array_fill(0, $argc, array());

  // iterate over all items
  for ($v = 0; $v < $argc; $v++){
    for ($h = 0; $h < $maxCount; $h++){
      $result[$h][$v] = isset($args[$v][$h]) ? $args[$v][$h] : null;
    }
  }

  return $result;
}

And example usage:

$newAry = array_rotate(
  array('a1','a2','a3'),
  array('b1','b2','b3'),
  array('c1','c2','c3')
);

// above yields:
Array
(
    [0] => Array
        (
            [0] => a1
            [1] => b1
            [2] => c1
        )

    [1] => Array
        (
            [0] => a2
            [1] => b2
            [2] => c2
        )

    [2] => Array
        (
            [0] => a3
            [1] => b3
            [2] => c3
        )

)

And in cases of uneven arrays, null is returned:

print_r(array_rotate(
  array('a1','a2'),
  array('b1','b2','b3','b4'),
  array('c1','c2','c3')
));

// above yields:
Array
(
    [0] => Array
        (
            [0] => a1
            [1] => b1
            [2] => c1
        )

    [1] => Array
        (
            [0] => a2
            [1] => b2
            [2] => c2
        )

    [2] => Array
        (
            [0] => 
            [1] => b3
            [2] => c3
        )

    [3] => Array
        (
            [0] => 
            [1] => b4
            [2] => 
        )
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜