dongliehuan3925 2012-06-05 11:46
浏览 18
已采纳

PHP - 根据值构造多维数组

I have an array:

$initialarray = array(
  0 = array(
    'unit' => 1,
    'class' => 1,
    'value' => 'string1'
  ),
  1 = array(
    'unit' => 1,
    'class' => 2,
    'value' => 'string2'
  ),
  2 = array(
    'unit' => 1,
    'class' => 2,
    'value' => 'string3'
  ),
  3 = array(
    'unit' => 2,
    'class' => 1,
    'value' => 'string4'
  )
  4 = array(
    'unit' => 2,
    'class' => 2,
    'value' => 'string5'
  )
);

What would be the best way to structure it (to group the resulting sub-arrays) depending first on the 'unit' field's values, and then depending on the 'class' field's values, like so:

$resultarray = array(
  // array of all the sub-arrays of 'unit' = 1
  $unit[1] = array(
    // array of all the sub-arrays of 'unit' = 1 and 'class' = 1
    $class[1] = array(
      0 = array(
        'unit' => 1,
        'class' => 1,
        'value' => 'string1'
      )
    )
    // array of all the sub-arrays of 'unit' = 1 and 'class' = 2
    $class[2] = array(
      0 = array(
        'unit' => 1,
        'class' => 2,
        'value' => 'string2'
      ),
      1 = array(
        'unit' => 1,
        'class' => 2,
        'value' => 'string3'
      )
    )
  )
  // array of all the sub-arrays of 'unit' = 2
  $unit[2] = array(
    // array of all the sub-arrays of 'unit' = 2 and 'class' = 1
    $class[1] = array(
      0 = array(
        'unit' => 2,
        'class' => 1,
        'value' => 'string4'
      )
    )
    // array of all the sub-arrays of 'unit' = 2 and 'class' = 2
    $class[2] = array(
      0 = array(
        'unit' => 2,
        'class' => 2,
        'value' => 'string5'
      )
    )
  )
)

I have asked a similar question here and got a working answer for only one iteration, i.e. for only structuring the array by one of the fields. But I could not make the same solution work for multiple iterations, i.e. for more than one field.

Also, is there a solution to structure a multidimensional array depending on more than two fields?

  • 写回答

3条回答 默认 最新

  • donglu2008 2012-06-05 13:00
    关注

    I think it's not a way of asking the question. It is very simple , you can do this by playing with arrays,keys and etc.... So first you should try hard for the problem. After If you have any problem in the middle of your tries then you can ask that here. I have solved your problem here is the complete code , but next time please do some work and then only post the problem. Never ask for the code.

    foreach ($initialarray as $key1=>$val1)
    {
        foreach ($val1 as $key2=>$val2)
        {            
    
            if($key2=='unit')
            {
                $num=$val2;
                if($val2!=$num)
                $testarr['unit'.$val2]=array();
            }
            if($key2=='class')
            {
                $testarr['unit'.$num]['class'.$val2][]=$val1;
            }
    
        }
    }
    
    print_r($testarr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?