从现有阵列创建新阵列


create new array from existing 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
   )
)

我怎样才能做到这一点?

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

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];
        }
    }
}

假设你想用数组中的最后一个值填充不均匀的数组:

$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']);

您可以使用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
        )
)

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

$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);