I just found out how to loop an array based on another array, but my problem is that if the second array is just 1 object than it works fine but I want to make it work with two objects.
Here is an example how it do works,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
print_r($data);
But I want to make it work with this one,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'NYCT01' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'NYCT01' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as $firstArray) {
foreach($firstArray as &$array){
// merge to get set members
$array = array_merge($shorten, $array);
}
}
unset($array);
print_r($data);
And this is what I want it to be,
Array
(
[Hertz] => Array
(
[ECAR] => 49.21
[CCAR] => 71.04
[ICAR] => 89.58
[SCAR] => 100.00
[FCAR] => n/a
[PCAR] => n/a
)
[Avis] => Array
(
[ECAR] => 412.00
[CCAR] => 347.00
[ICAR] => 285.00
[SCAR] => 224.00
[FCAR] => 165.00
[PCAR] => 100.00
)
)