<?php
$a = [
'owner' => [
'table' => 'owner',
'cols' => [
'name' => 'Name',
'address' => [
'table' => 'Address',
'cols' => [
'line1' => 'Street Line 1',
'line2' => 'Street Line 2',
'city' => 'City',
'state' => [
'table' => 'State',
'cols' => [
'stateName' => 'State'
]
],
'postal_code' => 'Postal Code',
]
]
]
],
'renter' => [
'table' => 'renter',
'cols' => [
'name' => 'Name',
'address' => [
'table' => 'Address',
'cols' => [
'line1' => 'Street Line 1',
'line2' => 'Street Line 2',
'city' => 'City',
'state' => [
'table' => 'State',
'cols' => [
'stateName' => 'State'
]
],
'postal_code' => 'Postal Code',
]
]
]
]
];
function flatten(array $a, string $path = '') {
if (isset($a['cols'])) {
return flatten($a['cols'], $path);
}
$pairs = [];
foreach ($a as $k => $v) {
$p = empty($path) ? $k : "{$path}.{$k}";
if (is_array($v)) {
array_push($pairs, ...flatten($v, $p));
} else {
$pairs[] = ["key" => $p, "label" => $v];
}
}
return $pairs;
}
var_dump(flatten($a));
/*
[
[
'key' => 'owner.name',
'label' => 'Name'
],
[
'key' => 'owner.address.line1',
'label' => 'Street Line 1'
],
[
'key' => 'owner.address.line2',
'label' => 'Street Line 2'
],
[
'key' => 'owner.address.city',
'label' => 'City'
],
[
'key' => 'owner.address.state.stateName',
'label' => 'State'
],
[
'key' => 'owner.address.postal_code',
'label' => 'Postal Code'
],
]
*/
- Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(12) {
[0]=>
array(2) {
["key"]=>
string(10) "owner.name"
["label"]=>
string(4) "Name"
}
[1]=>
array(2) {
["key"]=>
string(19) "owner.address.line1"
["label"]=>
string(13) "Street Line 1"
}
[2]=>
array(2) {
["key"]=>
string(19) "owner.address.line2"
["label"]=>
string(13) "Street Line 2"
}
[3]=>
array(2) {
["key"]=>
string(18) "owner.address.city"
["label"]=>
string(4) "City"
}
[4]=>
array(2) {
["key"]=>
string(29) "owner.address.state.stateName"
["label"]=>
string(5) "State"
}
[5]=>
array(2) {
["key"]=>
string(25) "owner.address.postal_code"
["label"]=>
string(11) "Postal Code"
}
[6]=>
array(2) {
["key"]=>
string(11) "renter.name"
["label"]=>
string(4) "Name"
}
[7]=>
array(2) {
["key"]=>
string(20) "renter.address.line1"
["label"]=>
string(13) "Street Line 1"
}
[8]=>
array(2) {
["key"]=>
string(20) "renter.address.line2"
["label"]=>
string(13) "Street Line 2"
}
[9]=>
array(2) {
["key"]=>
string(19) "renter.address.city"
["label"]=>
string(4) "City"
}
[10]=>
array(2) {
["key"]=>
string(30) "renter.address.state.stateName"
["label"]=>
string(5) "State"
}
[11]=>
array(2) {
["key"]=>
string(26) "renter.address.postal_code"
["label"]=>
string(11) "Postal Code"
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(12) {
[0]=>
array(2) {
["key"]=>
string(10) "owner.name"
["label"]=>
string(4) "Name"
}
[1]=>
array(2) {
["key"]=>
string(19) "owner.address.line1"
["label"]=>
string(13) "Street Line 1"
}
[2]=>
array(2) {
["key"]=>
string(19) "owner.address.line2"
["label"]=>
string(13) "Street Line 2"
}
[3]=>
array(2) {
["key"]=>
string(18) "owner.address.city"
["label"]=>
string(4) "City"
}
[4]=>
array(2) {
["key"]=>
string(29) "owner.address.state.stateName"
["label"]=>
string(5) "State"
}
[5]=>
array(2) {
["key"]=>
string(25) "owner.address.postal_code"
["label"]=>
string(11) "Postal Code"
}
[6]=>
array(2) {
["key"]=>
string(11) "renter.name"
["label"]=>
string(4) "Name"
}
[7]=>
array(2) {
["key"]=>
string(20) "renter.address.line1"
["label"]=>
string(13) "Street Line 1"
}
[8]=>
array(2) {
["key"]=>
string(20) "renter.address.line2"
["label"]=>
string(13) "Street Line 2"
}
[9]=>
array(2) {
["key"]=>
string(19) "renter.address.city"
["label"]=>
string(4) "City"
}
[10]=>
array(2) {
["key"]=>
string(30) "renter.address.state.stateName"
["label"]=>
string(5) "State"
}
[11]=>
array(2) {
["key"]=>
string(26) "renter.address.postal_code"
["label"]=>
string(11) "Postal Code"
}
}
preferences:
182.54 ms | 405 KiB | 212 Q