-1

I have two arrays, and a function.

private $configuration;
private $parameters;

private function setParameters() {
    // 
}

Inside the setParameters function, I want to assign elements to the parameters array. Both of these variables are arrays, and I need to copy the contents from configuration to parameters.

Configuration array works a little different, inside the configuration are nested arrays, the array configuration merely contains file names, inside that are nested arrays, similar to Laravel's config.

So, here is an example of how my configuration structure may look, sorry if you notice any mistakes, I hand wrote this without a text editor or IDE.

These arrays may go much further in terms of nesting, I just kept it simple here for readability.

[   
    "config_file_1" => [
        [
            "name" => "Example",
            "author" => "John"
        ]
    ],

    "config_file_2" => [
        [
            "country" => "Japan",
            "city" => "Tokyo"
        ]
    ],
]

This would need to be the equifalent to the code below.

$this->parameters["config_file_1.name"] = "Example";
$this->parameters["config_file_1.author"] = "John";
$this->parameters["config_file_2.country"] = "Japan"; // ETC, ETC...
6
  • I still don't get what you are trying to ask and what errors you are facing... Commented Aug 1, 2018 at 8:19
  • I'm not facing an error, I'm asking how I can do this. I am stuck on how I can do this, I don't know how to begin. Commented Aug 1, 2018 at 8:19
  • Not 100% sure what you want to achieve with the whole thing... but if I understand you correctly you don't need the parameters array in a good code... you could build something like $this->config_file[1]->name and so on... Commented Aug 1, 2018 at 8:24
  • I'm unsure, how is the question not simple enough for you to understand, is there a way I can simplify it? It seems self explanatory to me. Commented Aug 1, 2018 at 8:26
  • What's the point of having another array with same values? Commented Aug 1, 2018 at 8:27

1 Answer 1

1
              $configuration = [
        'config_file_1'=> [
            "name" => "Example",
            "author" => ['John', 'Do', 'Ou']],
        'config_file_2' => [
            "country" => "Japan",
            "city" => ["1", '2', '3'],
            'val' => ['a'=>'x', 'b'=> 'y']]
    ];

        $parameters = [];

    foreach($configuration  as $con => $config){
        foreach($config as $key => $value){
            if(is_array($value)){
                foreach($value as $keys => $value){
                    $parameters["{$con}.{$key}.{$keys}"] = $value;
            }} else {
                $parameters["{$con}.{$key}"] = $value;
            }
        }
    }

        print_r($parameters);
//        Array
//        (
//            [config_file_1_name] => Example
//            [config_file_1_author] => John
//            [config_file_2_country] => Japan
//            [config_file_2_city] => Tokyo
//        )
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was after, except it only goes one level deep, if for example country was another array (a nested array, inside a nested array) it would return an array object, and not the string conversion. If I try going a level deeper with another array, it will throw a Array to string conversion error.