0

Consider these three arrays:

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

$Trailers: Serial=>4321 Length=>42

$Forklifts: Serial=>5678 ForkLength=>24

What I'm looking to do is end up with a single array, which would end up with the columns Serial, Wheels, Color, Length, ForkLength

And the contents should end up looking like this

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

Is this possible? I have tried the following code, but I end up with weird results, like some of the columns duplicating for trailers and forklifts.

$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;
JustinCredible
  • 61
  • 1
  • 11

3 Answers3

2

You can collect the keys from your various input arrays using array_merge to generate the columns. (If you already have an array with a list of columns, you don't need to do this part.)

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

Then make a row template from the columns filled with null values.

$template = array_fill_keys($columns, null);

Your final array can be constructed by merging the input arrays onto the template.

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

array_merge($template, $array); will merge replace the null values in $template with the values of any of the corresponding keys that are defined in $array.

Executable example at 3v4l.org.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • I think OP want a linear array, no? – Syscall Mar 20 '18 at 21:57
  • @Syscall I was thinking they were trying to produce a multidimensional array based on the example under "And the contents should end up looking like this", although I could be mistaken. It's hard to tell, because it looks like the code they're currently using just overwrites the same array repeatedly. – Don't Panic Mar 20 '18 at 21:59
  • yes it is supposed to be multidim with the first row as the keys, and the rest as the data – JustinCredible Mar 20 '18 at 23:15
1

You're very close. There is a bug in all of your foreach loops. The keys of your $columns array are just numbers (it is an indexed array), so you should do something like this:

foreach($columns as $column) {
    $finalArray[$column] = $truckinfoData[$column];
}

And similarly for the other loops.

mkasberg
  • 16,022
  • 3
  • 42
  • 46
1

You could loop over the keys, create an array, and fill with NULL is the key doesn't exists, or with the key exists, and add this array to the $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) ;

Outputs (reformatted) :

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

Or, without variable variables:

$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 ;
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • I got a very similar response at https://www.reddit.com/r/PHPhelp/comments/85wklk/combining_arrays_that_have_common_columns/dw1r9qw/ – JustinCredible Mar 21 '18 at 15:11