PHP将数组与公共列组合在一起

时间:2018-03-20 21:35:48

标签: php arrays

考虑这三个数组:

$Trucks: Serial=> 12345 Wheels => 4 Color => Black

$Trailers: Serial=>4321 Length=>42

$Forklifts: Serial=>5678 ForkLength=>24

我要做的是最后得到一个数组,最后会有列Serial, Wheels, Color, Length, ForkLength

内容应该最终看起来像这样

Serial,Wheels,Color,Length,ForkLength 
12345,4,Black,NULL,NULL 
4321,NULL,NULL,42,NULL 
5678,NULL,NULL,NULL,24

这可能吗?我已经尝试了以下代码,但我最终得到了奇怪的结果,比如一些列重复用于预告片和叉车。

$columns = Array("TraderCategory", "Make", "Model", "Year", "Last_Update", "VIN", "Trim", "Price", "Ext_Color", "Int_Color", "Engine", "HP", "Wheelbase", "Suspension", "KM", "Transmission", "Description", "NewUsed", "Torque", "Rear_Axle", "Front_Axle", "Differential", "Brakes", "StockNum", "ditl_Inventory", "new_truckStatus", "statuscode", "ditl_ShowonTrader", "new_truckId", "MainPic", "MainPicModified", "ExtraPics", "ExtraPicsModified", "Width", "Length");
foreach ($columns as $key => column) {
    $finalArray[$column] = $truckinfoData[$key];
}
    foreach ($columns as $key => column) {
    $finalArray[$column] = $trailerinfoData[$key];
}
    foreach ($columns as $key => column) {
    $finalArray[$column] = $forkliftinfoData[$key];
}
return $finalArray;

3 个答案:

答案 0 :(得分:2)

您可以使用array_merge从各种输入数组中收集密钥以生成列。 (如果您已经有一个包含列表列表的数组,则不需要执行此部分。)

$columns = array_keys(array_merge($trucks, $trailers, $forklifts));

然后从填充空值的列中创建一个行模板。

$template = array_fill_keys($columns, null);

可以通过将输入数组合并到模板上来构建最终数组。

foreach ([$trucks, $trailers, $forklifts] as $array) {
    $result[] = array_merge($template, $array);
}

array_merge($template, $array);将合并将$template中的空值替换为$array中定义的任何相应键的值。

Executable example at 3v4l.org.

答案 1 :(得分:1)

你非常接近。你所有的foreach循环都有一个bug。你的ipdb> from django.conf import settings ipdb> settings.EMAIL_BACKEND 'django.core.mail.backends.locmem.EmailBackend' ipdb> settings.EMAIL_FILE_PATH '/my/file/path' 数组的键只是数字(它是一个索引数组),所以你应该这样做:

$columns

同样适用于其他循环。

答案 2 :(得分:1)

你可以遍历键,创建一个数组,填充NULL是关键不存在,或者存在键,并将此数组添加到$finalArray

$Trucks = ['Serial'=>  12345, 'Wheels' => 4, 'Color' => 'Black'];
$Trailers = ['Serial'=> 4321, 'Length'=> 42];
$Forklifts = ['Serial'=> 5678, 'ForkLength' => 24];

// If keys should be dynamic:
$keys = array_keys(array_merge($Trucks, $Trailers, $Forklifts));

$finalArray = [$keys];
foreach (['Trucks', 'Trailers', 'Forklifts'] as $array_name) {
    $data = [];
    foreach($keys as $idx => $column) {
        $data[$idx] = NULL ;
        if (isset($$array_name[$column]))
            $data[$idx] = $$array_name[$column] ;
    }
    $finalArray[] = $data ;
}
print_r($finalArray) ;

输出(重新格式化):

Array (
    [0] => Array ([0] => Serial [1] => Wheels [2] => Color [3] => Length [4] => ForkLength )
    [1] => Array ([0] =>  12345 [1] =>      4 [2] => Black [3] =>   NULL [4] =>       NULL )
    [2] => Array ([0] =>   4321 [1] =>   NULL [2] =>  NULL [3] =>     42 [4] =>       NULL )
    [3] => Array ([0] =>   5678 [1] =>   NULL [2] =>  NULL [3] =>   NULL [4] =>         24 )
)

或者,没有变量变量:

$finalArray = [$keys];
foreach ([$Trucks, $Trailers, $Forklifts] as &$array) {
    $data = [];
    foreach($keys as $idx => $column) {
        $data[$idx] = NULL ;
        if (isset($array[$column]))
            $data[$idx] = $array[$column] ;
    }
    $finalArray[] = $data ;
}