Created
February 25, 2025 15:42
-
-
Save alkinoy/b1b9eae6809ddde7f0137fe32973b063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$a = [ | |
2023 => [ | |
'maj' => [ | |
12 => 13, | |
23 => 20 | |
], | |
'lipiec' => [ | |
5 => 14 | |
] | |
], | |
2024 => [ | |
'luty' => [ | |
23 => 66 | |
], | |
'grudzień' => [ | |
18 => 100, | |
26 => 5 | |
] | |
] | |
]; | |
$b = ['year', 'month', 'day']; | |
$c = [ | |
['year' => '2023', 'month' => 'maj', 'day' => 12, 'data' => 13], | |
['year' => '2023', 'month' => 'maj', 'day' => 23, 'data' => 20], | |
['year' => '2023', 'month' => 'lipiec', 'day' => 5, 'data' => 14], | |
['year' => '2024', 'month' => 'luty', 'day' => 23, 'data' => 66], | |
['year' => '2024', 'month' => 'grudzień', 'day' => 18, 'data' => 100], | |
['year' => '2024', 'month' => 'grudzień', 'day' => 26, 'data' => 5], | |
]; | |
function processNode($a, $b, &$row, &$rows, int $level = 1) | |
{ | |
if ((count($b) + 1) === $level) { | |
$row['data'] = $a; | |
$rows[] = $row; | |
return; | |
} | |
if (! is_array($a)) { | |
echo "Data size mismatch\n"; | |
return; | |
} | |
foreach ($a as $k => $v) { | |
$row[$b[$level - 1]] = $k; | |
processNode($v, $b, $row, $rows, $level + 1); | |
} | |
} | |
$row = []; | |
$rows = []; | |
processNode($a, $b, $row, $rows); | |
var_dump($rows); | |
$b = ['year', 'month',]; | |
$row = []; | |
$rows = []; | |
processNode($a, $b, $row, $rows); | |
var_dump($rows); | |
$b = ['year', 'month', 'sss', 'ddd']; | |
$row = []; | |
$rows = []; | |
processNode($a, $b, $row, $rows); | |
var_dump($rows); | |
Results: | |
/bin/php /home/ot/.config/JetBrains/PhpStorm2024.3/scratches/scratch_6.php | |
array(6) { | |
[0]=> | |
array(4) { | |
["year"]=> | |
int(2023) | |
["month"]=> | |
string(3) "maj" | |
["day"]=> | |
int(12) | |
["data"]=> | |
int(13) | |
} | |
[1]=> | |
array(4) { | |
["year"]=> | |
int(2023) | |
["month"]=> | |
string(3) "maj" | |
["day"]=> | |
int(23) | |
["data"]=> | |
int(20) | |
} | |
[2]=> | |
array(4) { | |
["year"]=> | |
int(2023) | |
["month"]=> | |
string(6) "lipiec" | |
["day"]=> | |
int(5) | |
["data"]=> | |
int(14) | |
} | |
[3]=> | |
array(4) { | |
["year"]=> | |
int(2024) | |
["month"]=> | |
string(4) "luty" | |
["day"]=> | |
int(23) | |
["data"]=> | |
int(66) | |
} | |
[4]=> | |
array(4) { | |
["year"]=> | |
int(2024) | |
["month"]=> | |
string(9) "grudzień" | |
["day"]=> | |
int(18) | |
["data"]=> | |
int(100) | |
} | |
[5]=> | |
array(4) { | |
["year"]=> | |
int(2024) | |
["month"]=> | |
string(9) "grudzień" | |
["day"]=> | |
int(26) | |
["data"]=> | |
int(5) | |
} | |
} | |
====================== | |
array(4) { | |
[0]=> | |
array(3) { | |
["year"]=> | |
int(2023) | |
["month"]=> | |
string(3) "maj" | |
["data"]=> | |
array(2) { | |
[12]=> | |
int(13) | |
[23]=> | |
int(20) | |
} | |
} | |
[1]=> | |
array(3) { | |
["year"]=> | |
int(2023) | |
["month"]=> | |
string(6) "lipiec" | |
["data"]=> | |
array(1) { | |
[5]=> | |
int(14) | |
} | |
} | |
[2]=> | |
array(3) { | |
["year"]=> | |
int(2024) | |
["month"]=> | |
string(4) "luty" | |
["data"]=> | |
array(1) { | |
[23]=> | |
int(66) | |
} | |
} | |
[3]=> | |
array(3) { | |
["year"]=> | |
int(2024) | |
["month"]=> | |
string(9) "grudzień" | |
["data"]=> | |
array(2) { | |
[18]=> | |
int(100) | |
[26]=> | |
int(5) | |
} | |
} | |
} | |
==================== | |
Data size mismatch | |
Data size mismatch | |
Data size mismatch | |
Data size mismatch | |
Data size mismatch | |
Data size mismatch | |
array(0) { | |
} | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment