简体   繁体   中英

How to populate missing array data by flipping 2nd and 3rd level keys?

I have a multidimensional array and I need to ensure that paired/related subarrays are generated in the deepest level of my array.

The idea is that subarrays [65][1] and [155][1] should exist. At the same time, subarrays [65][2] and [155][2] should exist.

In other words, [nn][1] and [nn][2] must exist.

I would like to be able to automatically add subarrays which do not exist. I do not know how to do it with PHP.

So, I am looking for a code which goes through the array and creates subarrays which do not exist.

Here is an example.

Array (
  [65] => Array (
    [1] => Array (
      [2] => Array (
        [points] => 0000000600
        [competition] => 0000000011
      )
    )
  )

  [155] => Array (
    [1] => Array (
      [2] => Array (
        [points] => 0000000900
        [competition] => 0000000011
      )
    )

    [2] => Array (
      [1] => Array (
        [points] => 0000000750
        [competition] => 0000000025
      )
    )
  )
}

However, the subarray [65][2] does not exist.

As said, I am looking for a code which goes through the array and creates subarrays which do not exist.

The result should look like this:

Array (
  [65] => Array (
    [1] => Array (
      [2] => Array (
        [points] => 0000000600
        [competition] => 0000000011
      )
    )

/* this should be added automatically */

    [2] => Array (
      [1] => Array (
        [points] => 0000000000
        [competition] => 0000000000
      )
    )
  )

/* */

  [155] => Array (
    [1] => Array (
      [2] => Array (
        [points] => 0000000900
        [competition] => 0000000011
      )
    )

    [2] => Array (
      [1] => Array (
        [points] => 0000000750
        [competition] => 0000000025
      )
    )
  )
}

Notice that the default data is assign to a new subarray which is assigned 2nd and 3rd level keys using the 3rd and 2nd level keys respectively.

If I understood you correctly, all the elements in your main array are themselves arrays, and each of them must have both a [1] and a [2] element. You can check for this with the array_key_exists() function. Something like:

foreach($rg as $rgkey) {
   if(!array_key_exists(1,$rg[$rgkey])) {
      /* code to initialize $rg[$rgkey][1] */ }
   if(!array_key_exists(2,$rg[$rgkey])) {
      /* code to initialize $rg[$rgkey][2] */ }
   }

Traverse the top two levels of your structure and then check for the expected third level key, if it doesn't exist, populate the new subarray with the default data using flipped keys.

Code: ( Demo )

foreach ($rg as $id1 => $level2) {
    foreach ($level2 as $id2 => $level3) {
        $id3 = key($level3);
        if (!isset($level2[$id3])) {
            $rg[$id1][$id3][$id2] = [
                'points' => '0000000000',
                'competition' => '0000000000'
            ];
        }
    }
}
var_export($rg);

Output:

array (
  65 => 
  array (
    1 => 
    array (
      2 => 
      array (
        'points' => '0000000600',
        'competition' => '0000000011',
      ),
    ),
    2 => 
    array (
      1 => 
      array (
        'points' => '0000000000',
        'competition' => '0000000000',
      ),
    ),
  ),
  155 => 
  array (
    1 => 
    array (
      2 => 
      array (
        'points' => '0000000900',
        'competition' => '0000000011',
      ),
    ),
    2 => 
    array (
      1 => 
      array (
        'points' => '0000000750',
        'competition' => '0000000025',
      ),
    ),
  ),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM