Skip to content
Advertisement

PHP array (yaml) – add or remove sections of config file

I have some software that uses a yaml config file and I want to dynamically add a list of registered users to the config file when they join and remove them when their accounts are deleted.

The config file is as follows:

    targets:
  users:
    - name: AlFredo01
      uid: a1fa36h2a5hbd6535c919402ba8cc837bd75abfae
      userinfo:
        - email: Alfredo01@gmail.com
        - gender: male
        - country: it
    - name: AlFredo02
      uid: jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
      userinfo:
        - email: Alfredo02@gmail.com
        - gender: male
        - country: gb

I effectively would like to be able to add in new names and remove by name or UID using a PHP script on register/archive.

All the info needed would be available on registration. On deleting, their UID/Name is stored in the DB exactly how it will be added to the config file.

Can this be achieved, please?

I’ve never used yaml files before. I’ve found php-yaml and I can parse the data in to an array and print or dump it.

 <?
$parsed = yaml_parse_file('config2.yml');
var_dump($parsed);
?>

The above does dump the data as you would expect. Like below

    Array
(
    [targets] => 
    [users] => Array
        (
            [0] => Array
                (
                    [name] => AlFredo01
                    [uid] => a1fa36h2a5hbd6535c919402ba8cc837bd75abfae
                    [userinfo] => Array
                        (
                            [0] => Array
                                (
                                    [email] => Alfredo01@gmail.com
                                )

                            [1] => Array
                                (
                                    [gender] => male
                                )

                            [2] => Array
                                (
                                    [country] => it
                                )

                        )

                )

            [1] => Array
                (
                    [name] => AlFredo02
                    [uid] => jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
                    [userinfo] => Array
                        (
                            [0] => Array
                                (
                                    [email] => Alfredo02@gmail.com
                                )

                            [1] => Array
                                (
                                    [gender] => male
                                )

                            [2] => Array
                                (
                                    [country] => gb
                                )

                        )

                )

        )

)

I’d like to be able to effectively remove a full block based on UID.

So if I wanted to remove Alfredo02, I’d remove this section

                [1] => Array
                (
                    [name] => AlFredo02
                    [uid] => jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10
                    [userinfo] => Array
                        (
                            [0] => Array
                                (
                                    [email] => Alfredo02@gmail.com
                                )

                            [1] => Array
                                (
                                    [gender] => male
                                )

                            [2] => Array
                                (
                                    [country] => gb
                                )

                        )

                )

        )

Based on UID: jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10

Additionally, I’d like to be able to add a full section in for a new user.

I’ve tried basic lines to get things like the array ID to start with but keep getting nothing returned.

$search = array_search('jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10', array_column($parsed, 'uid'));

Could anyone please help me with a method of removing and adding data? Once I can generate a new “output” with data added or removed, I can write that back to the config.yml file.

Advertisement

Answer

Through investigation and determination I have figured out a way which allows you to remove from the array and write back to the yml file with the removed block/user.

$search = print_r(array_search('jmed8d83m4mtjf7f7f8j299d8210sla9010labvab10', array_column($parsed[users], 'uid', true)));
unset($parsed[users][$search]);

echo '<pre>' , print_r($parsed) , '</pre>';

yaml_emit_file("config2.yml",$parsed);

This effectively allows you to search for your uid inside of users and then print the result on the page using echo, but you can remove this.

Then writing the whole output back to the yml file using yaml_emit_file.

The next stage will be working out how to add a new user in to the array and write that back where necessary.

I’ll update when I get to that point for the benefit of others.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement