phparraysmultidimensional-arrayflatten

Convert nested array to multiple flat arrays


I don't found here a solution for my problem. Because of this, here my issue: I have an array of some parameters (like a YAML config file) and this need to be written to database finaly with primary ids and so on.

For example this is a part of my array:

$settings = [
'basic' => [                       // first level category
    'installation_type' => [       // setting parameter with parameter attributes
        'type' => '["single","cluster"]',
        'description' => 'bla blah',
        'readonly' => false,
        'hidden' => false,
        'trigger' => null,
        'default' => 'single'
    ],
    'db_master_host' => [
        'type' => 'ip',
        'description' => 'Database hostname or IP',
        'default' => 'localhost'
    ],
    'db_master_user' => [
        'type' => 'text',
        'description' => 'Database username',
        'default' => 'test'
    ],
    'db_master_pwd' => [
        'type' => 'secret',
        'description' => 'Database user password',
    ],
    'db_master_db' => [
        'type' => 'text',
        'description' => 'Database name',
        'default' => 'test'
    ]
],
'provisioning' => [         // first level category
    'snom' => [             // second level category
        'snom_prov_enabled' => [
            'type' => 'switch',
            'default' => false
        ],
        'snom_m3' => [
            'snom_m3_accounts' => [
                'type' => 'number',
                'description' => 'bla blah',
                'default' => '0'
            ]
        ],
        'snom_dect' => [
            'snom_dect_enabled' => [
                'type' => 'switch',
                'description' => 'bla blah',
                'default' => false
            ]
        ]
    ],
    'yealink' => [
        'yealink_prov_enabled' => [
            'type' => 'switch',
            'default' => false
        ]
    ]
],

];

As result I need some arrays like images of database tables (category, setting, value) with self-generated IDs:

$category["basic"] = [id: 1, parent: 0, name: "basic", order: 1]
$setting["installation_type"] = [id: 1, catId: 1, name: "installation_type", type: '["single","cluster"]', desc: 'asd', order: 1]
$value["installation_type"] = [id: 1, setId: 1, default: 'single']
$setting["db_master_host"] = [id: 2, catId: 1, name: "db_master_host", type: 'ip', desc: 'asd', order: 2]
$value["db_master_host"] = [id: 2, setId: 2, name: "db_master_host", default: 'localhost']
...
$category["provisioning"] = [id: 2, parent: 0, name: "provisioning", order: 2]
$category["snom"] = [id: 3, parent: 2, name: "snom", order: 3]
...

I hope my problem is understandable here and anyone could help me.


Solution

  • You can handle that nested config by walking through it recursively: whenever the current node owns a type key you treat it as a setting, otherwise it’s a category and you dive deeper. Keep two counters one for categories, one for settings and bump the right one each time you create a record so every row gets a predictable primary key. Within each level track an order integer as you iterate, preserving the original sequence for the UI. While you traverse, fill three arrays on the fly: one for categories (id, parent, name, order), one for settings (id, catId, name, type, desc, order), and one for default values (id, setId, default). A compact closure:

    $catId = $setId = 0;
    $categories = $settingsTbl = $valuesTbl = [];
    
    $walk = function (array $node, int $parent = 0) use (&$walk, &$catId, &$setId,
            &$categories, &$settingsTbl, &$valuesTbl) {
    
        $order = 1;
        foreach ($node as $key => $val) {
            if (is_array($val) && array_key_exists('type', $val)) {
                $settingsTbl[$key] = [
                    'id'    => ++$setId,
                    'catId' => $parent,
                    'name'  => $key,
                    'type'  => $val['type'],
                    'desc'  => $val['description'] ?? null,
                    'order' => $order,
                ];
                $valuesTbl[$key] = [
                    'id'      => $setId,
                    'setId'   => $setId,
                    'default' => $val['default'] ?? null,
                ];
            } else {
                $categories[$key] = [
                    'id'     => ++$catId,
                    'parent' => $parent,
                    'name'   => $key,
                    'order'  => $order,
                ];
                $walk($val, $catId);
            }
            $order++;
        }
    };
    
    $walk($settings);   // original config array
    

    run this, then insert the three resulting arrays into your tables inside a single transaction; you’ll end up with clean, flat rows that still reference their original hierarchy through parent and catId.