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) { $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' ], ] */

preferences:
68.87 ms | 402 KiB | 5 Q