试图破坏与$ key连接的多维数组。$ subkey1。$ subkey2 ... $ subkeyN = $ value

I have a small problem with my recursive function, and it is that FQDN is not being created correctly.

public function testBuildReverseSchemaNames() {
    $input = [
        'propertyZeroSchema' => 'value_0',
        'communicationPreferences' => [
            'grv3_newsletter_bi' => true
        ],
        'someContainer' => [
            'propertyOneSchema' => 'value_1'
        ],
        'propertyThreeSchema' => 'value_3'
    ];


    $expected = [
        'propertyZeroSchema' => 'value_0',
        'communicationPreferences.grv3_newsletter_bi' => true,
        'someContainer.propertyOneSchema' => 'value_1',
        'propertyThreeSchema' => 'value_3'
    ];

    $output = [];
    $this->buildReverseSchemaNames($input, $output);

    $this->assertEquals($expected, $output);
}


public function buildReverseSchemaNames($data, &$output, $currentFqdn = '') {
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $currentFqdn .= $key.'.';
            $this->buildReverseSchemaNames($value, $output, $currentFqdn);
        } else {
            $currentFqdn .= $key;
            $output[$currentFqdn] = $value;
        }
    }
}

But the output is like this:

Array (
    'propertyZeroSchema' => 'value_0'
    'propertyZeroSchemacommunicationPreferences.grv3_newsletter_bi' => true
    'propertyZeroSchemacommunicationPreferences.someContainer.propertyOneSchema' => 'value_1'
    'propertyZeroSchemacommunicationPreferences.someContainer.propertyThreeSchema' => 'value_3'
)

Well, I was able to find the answer by myself:

Edit: After I thought about doing it cleaner, @jh1711 just answered this. If you repost it I will accept your answer.

public function buildReverseSchemaNames($data, &$output, $currentFqdn = '') {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $this->buildReverseSchemaNames($value, $output, $currentFqdn . $key.'.');
            } else {
                $output[$currentFqdn.$key] = $value;
            }
        }
    }

You should not modify $currentFqdn inside your function. Otherwise your changes will affect later iteration of the foreach loop. It should work, if you modify your code like this:

public function buildReverseSchemaNames($data, &$output, $currentFqdn = '') {
  foreach ($data as $key => $value) {
    if (is_array($value)) {
      $this->buildReverseSchemaNames($value, $output, $currentFqdn.$key.'.');
    } else {
      $output[$currentFqdn.$key] = $value;
    }
  }
}