PHP同步数组键

Lets say that we have dynamically generated array.

    $arr[1]["a"] = "value";
    $arr[1]["b"] = "value";
    $arr[1]["c"] = "value";

    $arr[2]["a"] = "value";
    $arr[2]["b"] = "value";
    $arr[2]["c"] = "value";
    $arr[2]["d"] = "value";

    $arr[3]["a"] = "value";
    $arr[3]["g"] = "value";

Generating the Array can be manipulated, so do not take this example like a core lines. As you see there is different keys, but at the end we must get:

$arr[1]['a'] = 'value';
$arr[1]['b'] = 'value';
$arr[1]['c'] = 'value';
$arr[1]['d'] = 'empty value';
$arr[1]['g'] = 'empty value';

$arr[2]['a'] = 'value';
$arr[2]['b'] = 'value';
$arr[2]['c'] = 'value';
$arr[2]['d'] = 'value';
$arr[2]['g'] = 'empty value';

$arr[3]['a'] = 'value';
$arr[3]['b'] = 'empty value';
$arr[3]['c'] = 'empty value';
$arr[3]['d'] = 'empty value';
$arr[3]['g'] = 'value';

non empty values are different, so array_merge is not so using good idea.

I think you want this (this is horribly inefficient but my brain is struggling)

$keys = array();
foreach($arr as $array){
   $keys = array_merge($keys, array_keys($array));
}
//$keys now has all unique keys
foreach($arr as $array){
   foreach($keys as $key){
      if(!isset($array[$key])){$array[$key] = null}
   }
}

This is untested but i think it should work

I Guess is you want to fill your array with default values ... you can use array_fill to do that

$keys = array("a","b","c","d","g");
$arr[1] = array_combine($keys,array_fill(0, 5, 'empty value'));


$arr[1]["a"] = "value";
$arr[1]["b"] = "value";
$arr[1]["c"] = "value";


print_r($arr[1]);

Output

Array
(
    [a] => value
    [b] => value
    [c] => value
    [d] => empty value
    [g] => empty value
)