Skip to content

Instantly share code, notes, and snippets.

@kalimatas
Created August 13, 2016 18:57
Show Gist options
  • Save kalimatas/d83d77d6b17758922755830f7bfca99b to your computer and use it in GitHub Desktop.
Save kalimatas/d83d77d6b17758922755830f7bfca99b to your computer and use it in GitHub Desktop.
Nested array creation
<?php
$taxes = [
[
'country_id' => 'a',
'value' => 42,
'type' => 't1',
],
[
'country_id' => 'a',
'value' => 88,
'type' => 't2',
],
[
'country_id' => 'b',
'value' => 10,
'type' => 't1',
],
];
$countries = [
[
'id' => 'a',
'code' => 'a_code',
],
[
'id' => 'b',
'code' => 'b_code',
],
[
'id' => 'c',
'code' => 'c_code',
],
];
$countryTaxMap = [
'd_code' => [
't4' => 8,
],
];
foreach ($taxes as $tax) {
foreach ($countries as $country) {
if ($tax['country_id'] == $country['id']) {
$countryTaxMap[$country['code']][$tax['type']] = $tax['value'];
}
}
}
print_r($countryTaxMap);
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment