3v4l.org

run code in 300+ PHP versions simultaneously
<?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) { if (is_array($v)) { array_push($pairs, ...flatten($v, "{$path}.{$k}")); } else { $pairs[] = ["key" => "{$path}.{$k}", "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.25, 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 - 8.3.7
array(12) { [0]=> array(2) { ["key"]=> string(11) ".owner.name" ["label"]=> string(4) "Name" } [1]=> array(2) { ["key"]=> string(20) ".owner.address.line1" ["label"]=> string(13) "Street Line 1" } [2]=> array(2) { ["key"]=> string(20) ".owner.address.line2" ["label"]=> string(13) "Street Line 2" } [3]=> array(2) { ["key"]=> string(19) ".owner.address.city" ["label"]=> string(4) "City" } [4]=> array(2) { ["key"]=> string(30) ".owner.address.state.stateName" ["label"]=> string(5) "State" } [5]=> array(2) { ["key"]=> string(26) ".owner.address.postal_code" ["label"]=> string(11) "Postal Code" } [6]=> array(2) { ["key"]=> string(12) ".renter.name" ["label"]=> string(4) "Name" } [7]=> array(2) { ["key"]=> string(21) ".renter.address.line1" ["label"]=> string(13) "Street Line 1" } [8]=> array(2) { ["key"]=> string(21) ".renter.address.line2" ["label"]=> string(13) "Street Line 2" } [9]=> array(2) { ["key"]=> string(20) ".renter.address.city" ["label"]=> string(4) "City" } [10]=> array(2) { ["key"]=> string(31) ".renter.address.state.stateName" ["label"]=> string(5) "State" } [11]=> array(2) { ["key"]=> string(27) ".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(11) ".owner.name" ["label"]=> string(4) "Name" } [1]=> array(2) { ["key"]=> string(20) ".owner.address.line1" ["label"]=> string(13) "Street Line 1" } [2]=> array(2) { ["key"]=> string(20) ".owner.address.line2" ["label"]=> string(13) "Street Line 2" } [3]=> array(2) { ["key"]=> string(19) ".owner.address.city" ["label"]=> string(4) "City" } [4]=> array(2) { ["key"]=> string(30) ".owner.address.state.stateName" ["label"]=> string(5) "State" } [5]=> array(2) { ["key"]=> string(26) ".owner.address.postal_code" ["label"]=> string(11) "Postal Code" } [6]=> array(2) { ["key"]=> string(12) ".renter.name" ["label"]=> string(4) "Name" } [7]=> array(2) { ["key"]=> string(21) ".renter.address.line1" ["label"]=> string(13) "Street Line 1" } [8]=> array(2) { ["key"]=> string(21) ".renter.address.line2" ["label"]=> string(13) "Street Line 2" } [9]=> array(2) { ["key"]=> string(20) ".renter.address.city" ["label"]=> string(4) "City" } [10]=> array(2) { ["key"]=> string(31) ".renter.address.state.stateName" ["label"]=> string(5) "State" } [11]=> array(2) { ["key"]=> string(27) ".renter.address.postal_code" ["label"]=> string(11) "Postal Code" } }

preferences:
158.14 ms | 405 KiB | 154 Q