1

I'm working with building associative arrays using recursive function which involves carrying the path through each instance for pushing values to nested array of key

So we feed our function a base array containing 3 keys with empty arrays as values

$fooArray = array(2 => array(), 14 => array(), 8 => array());
$ids = array(2, 14, 8);
function test($fooArray, $ids) {

Our recursive function starts with looping those ids, checking for subids of each and then adding that array filled with values to key

$subids = array(5, 8, 9);
$subkeys = array_fill_keys($subids, array());
$fooArray[$id] = $subkeys;

On the second run of our recursive function, we loop through the newly added sub set of keys and rerun the process. The issue is I'm losing my parent path.

Now I can send our first $id through the function as a parent id and then push

$fooArray[$pid][$id] = $subkeys

But now what do I do for a third and fourth run? What I need is a way to build that path and carry it through each function

$path = $fooArray[$pid][$id];
$path = $fooArray[$pid][$pid][$id];

This is my best attempt

function rcr(&$fooArray, $ids, $path, $i = 0) {
   if ($ids and count($ids) > 0) {
     foreach( $ids as $id ) {
         $subids = // function that gets our array of subids
         $subkeys = array_fill_keys($subids);
         if ($i == 0) {
            $fooArray[$id] = $subkeys;
            $path = &$fooArray[$id];
         } else {
            $path[$id] = $subkeys;
            $path = &$path[$id];
         }
         $s = $i + 1;
         function rcr($fooArray, $subids, $path, $s);
     }
  }
}

function get_rcr() {
   $fooArray = array(2 => array(), 14 => array(), 8 => array());
   $ids = array(2, 14, 8);
   $path = "";
   rcr($fooArray, $ids, $path, $i = 0);
   return $fooArray;
}

But on the second run of the function, $path[$id] fails to add to the main $fooArray

The desired result is

array(
  [2] => array(
      [5] => array(
          [7] => array(
               [46] => array()
          )
      ),
      [108] => array()
  ), 
  [14] => array(),
  [8] => array(
      [72] => array()
  )
)

So how?

Leopold
  • 27
  • 5
  • 2
    Could you make it more explicit what your input is and what result you're looking for? At least some examples. – Jeto Jan 04 '20 at 09:20
  • There are a few issues here: you should call rcr in rcr to get recursion, ie remove function keyword from function body. You call rcr in get_rcr without arguments. – PeterM Jan 04 '20 at 09:23
  • Also this is invalid syntax (can't have `function rcr($fooArray, $subids, $path, $s);` in the middle of your code, guess that `function` keyword shouldn't be there). – Jeto Jan 04 '20 at 09:28
  • Where are these IDs such as 5, 7, and 46 coming from? – Jeto Jan 04 '20 at 09:31
  • Does that matter @Jeto? It comes from another function that gets subids of id – Leopold Jan 04 '20 at 09:32

1 Answers1

1

I'm not entirely sure I understood the question completely, but here goes.

You could make use of a far simpler approach, by only taking an array of IDs as parameter and filling an array where:

  • each key is the ID,
  • and each value is the result of the following iteration, based on that ID's sub IDs.

Function:

function rcr(array $ids): array
{
  $array = [];
  foreach ($ids as $id) {
    $array[$id] = rcr(getSubIds($id));
  }
  return $array;
}

Where getSubIds is that other function that gets sub IDs of an ID. Here's a dummy implementation of it following your sample:

function getSubIds(int $id): array
{
  switch ($id) {
    case 2: return [5, 108];
    case 5: return [7];
    case 7: return [46];
    case 8: return [72];
    default: return [];
  }
}

Usage:

$result = rcr([2, 14, 8]);

Demo: https://3v4l.org/tjMRa

Jeto
  • 14,596
  • 2
  • 32
  • 46