2

In php, I need to turn this array:

array(
    'group1_selector1_rule1' => value
,   'group1_selector1_rule2' => value
,   'group1_selector1_rule3' => value
,   'group1_selector2_rule1' => value
,   'group1_selector2_rule2' => value
,   'group2_selector1_rule1' => value
,   'group2_selector1_rule2' => value
,   'group3_selector1_rule1' => value
,   'group3_selector2_rule1' => value
,   'group3_selector2_rule2' => value
);

into this:

array(
    'group1' => array(

        'selector1' => array(
            'rule1' => value
        ,   'rule2' => value
        ,   'rule3' => value
        )

    ,   'selector2' => array(
            'rule1' => value
        ,   'rule2' => value
        )

    ) // end group 1

,   'group2' => array(

        'selector1' => array(
            'rule1' => value
        ,   'rule2' => value
        )
    ) // end group 2

,   'group3' => array(

        'selector1' => array(
            'rule1' => value
        )

    ,   'selector2' => array(
            'rule1' => value
        ,   'rule2' => value
        )
    ) // end group 3
); // end array

If anyone out there would like to suggest a smart way, I'd be interested :) I think I could labour some algorithm, but I'd like something elegant and have a feeling that some clever use of explode() and some 'array_merging' functions could do it?

3 Answers 3

4
  $res_array = array();
  foreach ($array as $key=>$value) {
    $x = explode("_", $key);
    $res_array[$x[0]][$x[1]][$x[2]] = $value;
    }

  var_dump($res_array);

for $array

$array = array(
    'group1_selector1_rule1' => 'value1'
,   'group1_selector1_rule2' => 'value2'
,   'group1_selector1_rule3' => 'value3'
,   'group1_selector2_rule1' => 'value4'
,   'group1_selector2_rule2' => 'value5'
,   'group2_selector1_rule1' => 'value6'
,   'group2_selector1_rule2' => 'value7'
,   'group3_selector1_rule1' => 'value8'
,   'group3_selector2_rule1' => 'value9'
,   'group3_selector2_rule2' => 'value10'
);

Output:

array(3) {
  ["group1"]=>
  array(2) {
    ["selector1"]=>
    array(3) {
      ["rule1"]=>
      string(6) "value1"
      ["rule2"]=>
      string(6) "value2"
      ["rule3"]=>
      string(6) "value3"
    }
    ["selector2"]=>
    array(2) {
      ["rule1"]=>
      string(6) "value4"
      ["rule2"]=>
      string(6) "value5"
    }
  }
  ["group2"]=>
  array(1) {
    ["selector1"]=>
    array(2) {
      ["rule1"]=>
      string(6) "value6"
      ["rule2"]=>
      string(6) "value7"
    }
  }
  ["group3"]=>
  array(2) {
    ["selector1"]=>
    array(1) {
      ["rule1"]=>
      string(6) "value8"
    }
    ["selector2"]=>
    array(2) {
      ["rule1"]=>
      string(6) "value9"
      ["rule2"]=>
      string(7) "value10"
    }
  }
}
1
  • that was fast :) thanks a lot, saved me half an evening here! Commented Apr 12, 2011 at 21:50
2
<?

$poo=array(
    'group1_selector1_rule1' => 'value'
,   'group1_selector1_rule2' => 'value'
,   'group1_selector1_rule3' => 'value'
,   'group1_selector2_rule1' => 'value'
,   'group1_selector2_rule2' => 'value'
,   'group2_selector1_rule1' => 'value'
,   'group2_selector1_rule2' => 'value'
,   'group3_selector1_rule1' => 'value'
,   'group3_selector2_rule1' => 'value'
,   'group3_selector2_rule2' => 'value'
);

foreach ($poo as $k=>$v){
        list($a,$b,$c) = explode('_',$k);
        $new[$a][$b][$c]=$v;
}
print_r($new);

http://www.ideone.com/skv0a

1
  • thanks, and great tool you showed me ideone.com I like that it shows memory usage and execution time, makes it possible to compare solutions. sorry I can't accept both answers. this one seems slightly more elegant from the syntax, but I guess they do roughly the same. Commented Apr 12, 2011 at 21:52
2

How about a generic solution?

<?php
function transform($oldArray)
{
  $newArray = array();
  foreach ($oldArray as $selector => $value)
  {
    $parts = explode('_', $selector);
    $last = array_pop($parts);
    $arr = &$newArray;

    foreach ($parts as $part)
    {
      if (!is_array($arr[$part])) $arr[$part] = array();
      $arr = &$arr[$part];
    }
    $arr[$last] = $value;
  }
  return $newArray;
}
?>
1
  • thanks, good point regarding the generic vs. specific solutions. Commented Apr 12, 2011 at 21:53

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.