3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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' ], ] */
based on mijNX
Output for 7.1.25 - 7.3.1
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" } }