php字符串到数组结构

时间:2011-04-12 21:26:58

标签: php string multidimensional-array

在php中,我需要转换这个数组:

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
);

进入这个:

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

如果有人想建议一个聪明的方式,我会感兴趣:) 我想我可以使用一些算法,但是我想要一些优雅的东西,并且有一种感觉,一些聪明的使用explode()和一些'array_merging'函数可以做到吗?

3 个答案:

答案 0 :(得分: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'
);

<强>输出:

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 :(得分: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

答案 2 :(得分:2)

通用解决方案怎么样?

<?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;
}
?>