从现有数组创建新数组

时间:2015-02-02 08:53:08

标签: php arrays multidimensional-array

我有这样的多个数组:

array (
[floorBuildingName] => Array
    (
        [0] => Lt.1
        [1] => Lt.2
    )

[roomFloorName] => Array
    (
        [0] => Single
        [1] => Medium1
        [2] => MaXI
    )
)

我想将两个数组合并为一个数组。

例如:

array (
 [0] => array(
     [0] =>Lt.1,
     [1] =>Single
   ),
 [1] => array(
     [0] =>Lt.2,
     [1] =>Medium1
   ),
  [2] => array(
     [0] =>Lt.2,
     [1] =>MaXI
   )
)

我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:0)

您可以非常轻松地使用array_map执行此操作:

试试这段代码:

$arr1 = array(1, 2);
$arr2 = array('one', 'two', 'three', 'four');

while(count($arr1) != count($arr2)) {
    //If Array1 is Shorter then Array2
    if (count($arr1)<count($arr2)) {
        $arr1[] = $arr1[count($arr1) - 1];
    }
    //If Array2 is Shorter then Array1
    if (count($arr1) > count($arr2)) {
        $arr2[] = $arr2[count($arr2) - 1];
    }
}
//Now merge arrays
$newarray = (array_map(null, $arr1, $arr2));


print_r($newarray);

输出:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => one
        )

    [1] => Array
        (
            [0] => 2
            [1] => two
        )

    [2] => Array
        (
            [0] => 2
            [1] => three
        )

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

答案 1 :(得分:0)

有不同数量的参数的解决方案:

$floorBuildingName = array(
        'Lt.1',
        'Lt.2'
    );
$roomFloorName = array(
        'Single', 'Medium1', 'MaXI'
);

class ValueArrayIterator extends ArrayIterator
{

    protected $arrays;

    protected $latestValues = [];

    public function __construct(array $mainArray) {
        parent::__construct($mainArray);
        $this->arrays = func_get_args();
    }

    public function current()
    {
        $returnValue = [];

        foreach ($this->arrays as $arrayKey => $array) {
            if (isset($array[$this->key()])) {
                $this->latestValues[$arrayKey] = $array[$this->key()];
            }
            $returnValue[] = $this->latestValues[$arrayKey];
        }

        return $returnValue;
    }
}

$iterator = new ValueArrayIterator($roomFloorName, $floorBuildingName);
$newArray = iterator_to_array($iterator);

答案 2 :(得分:0)

首先,您必须确定最大数组长度。然后,创建一个新数组,最后,将给定索引处的元素放入新数组中。如果索引超出范围,则使用最后一个元素。

var $maxNumber = 0;
foreach ($myArray as $array) {
    $maxNumber = max($maxNumber, count($array));
}

$result = array();
for ($index = 0; $index < $maxNumber; $index++) {
    $result[] = array();
    foreach($myArray as $array) {
        if (count($array) < $maxNumber) {
            $result[$index][] = $array(count($array) - 1);
        } else {
            $result[$index][] = $array[$index];
        }
    }
}

答案 3 :(得分:0)

假设您要使用数组中的最后一个值填充不均匀的数组:

$data = ['floorBuildingName' => [..], ..];

// find the longest inner array
$max = max(array_map('count', $data));

// pad all arrays to the longest length
$data = array_map(function ($array) use ($max) {
    return array_pad($array, $max, end($array));
}, $data);

// merge them
$merged = array_map(null, $data['floorBuildingName'], $data['roomFloorName']);