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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算