1

I've created a function that returns a nested array based in the values and keys of another two arrays and assigns a third value to the deepest key (hope it makes any sense with the code):

function myfunc_build_array($mydata,$keys,$value)
{
    $newarr=array();

    foreach ($mydata as $data)
    {
        $evalvar='$newarr';     
        foreach ($keys as $key)
        {           
            $evalvar.="['".$data[$key]."']";
        }
        $evalvar.='=$value;';
        eval($evalvar); 
    }
    return $newarr;
}

So, i.e. if we have:

$mydata=array(
      0=>array('section'=>'NS1','subsection'=>'NS1A','employee'=>'2812','name'=>'Bob'),
      1=>array('section'=>'NS1','subsection'=>'NS1A','employee'=>'2811','name'=>'Bib'),
      2=>array('section'=>'NS1','subsection'=>'NS1B','employee'=>'2718','name'=>'Bub'),
    );

$keys= array('section','subsection','employee');

The result for myfunc_build_array($mydata,$keys,"active"); is:

array(1) {
  ["NS1"]=>
  array(2) {
    ["NS1A"]=>
    array(2) {
      [2812]=>
      string(6) "active"
      [2811]=>
      string(6) "active"
    }
    ["NS1B"]=>
    array(1) {
      [2718]=>
      string(6) "active"
    }
  }
}

It works fine but as I usually avoid using eval(), I was wondering if there is a better way to do it, more elegant or faster.

2 Answers 2

2

There is a way to do it without eval() by using references. Don't know if it's faster, but it's perhaps safer:

function myfunc_build_array($keySets,$keyIndices,$value)
{
    $slot = null;
    $newarr=array();
    foreach ($keySets as $keys)
    {
        $slot = &$newarr;
        foreach ($keyIndices as $keyIndex)
        {           
            $slot = &$slot[$keys[$keyIndex]];
        }
        $slot = $value;
    }
    return $newarr;
}
Sign up to request clarification or add additional context in comments.

Comments

1

This worked for me, although I wouldn't necessarily call it elegant:

function myfunc_build_array($mydata,$keys,$value)
{
    $newarr = array();

    end( $keys);
    $last_key = key( $keys);

    foreach ($mydata as $data)
    {  
        $temp =& $newarr; 
        foreach ($keys as $i => $key)
        {           
            $temp[$data[$key]] = isset( $temp[$data[$key]]) ? $temp[$data[$key]] : ($last_key === $i ? $value : array());
            $temp =& $temp[$data[$key]];
        }
    }

    return $newarr;
}

You can see that it produces this output:

array(1) {
  ["NS1"]=>
  array(2) {
    ["NS1A"]=>
    array(2) {
      [2812]=>
      string(6) "active"
      [2811]=>
      string(6) "active"
    }
    ["NS1B"]=>
    array(1) {
      [2718]=>
      string(6) "active"
    }
  }
}

3 Comments

Are there any potential problems if you don't explicitly create each level of the array and simply set a reference to $temp['level1'] where $temp = array(); In my experience, PHP just creates the key without throwing any errors when you do this.
Probably not - I always try to so it's more clear what's going on.
Gotcha. I guess it's probably also the safer thing to do since the PHP manual doesn't really specify what the standard behavior is when you do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.