You are here

function _authcache_enum_cartesian in Authenticated User Page Caching (Authcache) 7.2

Generate a cartesian product of the given associative array of arrays.

If the input looks like this: $sets = array( 'x' => array(1, 2, 3), 'y' => array('a', 'b'), );

The result consists of an array with 6 elements: $result = array( array('x' => 1, 'y' => 'a'), array('x' => 2, 'y' => 'a'), array('x' => 3, 'y' => 'a'), array('x' => 1, 'y' => 'b'), array('x' => 2, 'y' => 'b'), array('x' => 3, 'y' => 'b'), );

2 calls to _authcache_enum_cartesian()
AuthcacheEnumCartesianTestCase::testCombineK in modules/authcache_enum/lib/Drupal/authcache_enum/Tests/AuthcacheEnumCartesianTestCase.php
Test calculation of the cartesian product.
authcache_enum_user_keys in modules/authcache_enum/authcache_enum.module
Enumerate every possible authcache key for authenticated users.

File

modules/authcache_enum/authcache_enum.comb.inc, line 77
Private helper functions for the Authcache Enum module.

Code

function _authcache_enum_cartesian(array $sets) {
  $n = 1;
  $div = array();
  $mod = array();
  foreach ($sets as $set_id => $elements) {
    $c = count($elements);
    $div[$set_id] = $n;
    $mod[$set_id] = $c;
    $n *= $c;
  }
  $result = array();
  for ($i = 0; $i < $n; $i++) {
    foreach ($sets as $set_id => $elements) {
      $j = (int) ($i / $div[$set_id]) % $mod[$set_id];
      $result[$i][$set_id] = $elements[$j];
    }
  }
  return $result;
}