I have an array of arrays like this:
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
)
),
);
In the $data
array there are three arrays named data1, data2 and data3 respectively.
In each array, the first row is the name of the columns and the remaining rows are the values for those columns (think of it like a table).
In each data1,data2 and data3 the first row contains a column called ID.
I want to group the data from all three arrays based on the matching ID field such that the final output array is like this:
Desired Output:
$output =
array (
'output' =>
array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
3 => 'Balance',
4 => 'Active',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
3 => '4533',
4 => 'Yes',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
3 => 'null',
4 => 'No',
),
)
);
What I tried(just a attempt to combine data1 and data2) :
$d1=$data['data1'];
$d2=$data['data2'];
if(count($d1)>count($d2))
{
$arr1=array();
$arr2=array();
$arr3=array();
$column1=$d1[0];
$column2=$d2[0];
for($i=1;$i<=(count($d1)-1);$i++)
{
if($i<count($d2))
$arr2[]=array_combine($column2,$d2[$i]);
else
$arr2[]=array_combine($column2,array('0'=>'','1'=>''));
}
for($i=1;$i<=(count($d1)-1);$i++)
{
$arr1[]=array_combine($column1,$d1[$i]);
}
for($i=0;$i<=(count($arr1)-1);$i++)
{
$arr3[]=array_merge($arr1[$i],$arr2[$i]);
}
print_r($arr3);
}
I need help regarding a neat code to combine any number of arrays.
Notice that missing elements should receive a null value.
How do I get the output I have mentioned above?
This splits it into 2 steps, first accumulate all the data by the ID, also all the header columns are collected. Then use this data to create output array with blanks where the data is missing.
Comments in code...
$store = [];
$headers = [];
foreach ( $data as $set ) {
$headerRow = array_shift($set);
// Collect all header columns
$headers = array_merge($headers, $headerRow);
foreach ( $set as $index => $list ){
// Create associative list of data so they can be combined (i.e. ID fields)
$list = array_combine($headerRow, $list);
// Use ID value as key and create if needed
if ( !isset($store[$list["ID"]]) ) {
$store[$list["ID"]] = $list;
}
else {
$store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
}
}
}
$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow ) {
// Fill in the fields for this ID and then change to numeric keys
$output['output'][] = array_values(array_merge($blank, $dataRow));
}