本文介绍了集团由子阵列值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个有趣的挑战。我有子阵在以下格式的数组:

 阵列
(
    A =>阵列(n = 20,名称=黑猩猩)
    B =>阵列(n = 40,名称=会议)
    C =>阵列(n = 20,名称=王朝)
    D =>阵列(n = 50,名称=巧克力)
    E =>阵列(ID = 10,姓名=香蕉)
    F =>阵列(n = 50,名称=幻想)
    G =>阵列(n = 50,名称=足球)
)
 

和我想组到一个新的数组的基础上在每个子阵列的ID字段。

 阵列
(
    10 =>排列
          (
            E =>阵列(ID = 10,姓名=香蕉)
          )
    20 =>排列
          (
            A =>阵列(n = 20,名称=黑猩猩)
            C =>阵列(n = 20,名称=王朝)
          )
    40 =>排列
          (
            B =>阵列(n = 40,名称=会议)
          )
    50 =>排列
          (
            D =>阵列(n = 50,名称=巧克力)
            F =>阵列(n = 50,名称=幻想)
            G =>阵列(n = 50,名称=足球)
          )
)
 

谢谢!

解决方案

  $ ARR =阵列();

的foreach($ old_arr为$关键=> $项)
{
   $ ARR [$项目['身份证'] [$关键] = $项目;
}

ksort($改编,SORT_NUMERIC);
 

Here's an interesting challenge. I have an array of subarrays in the following format:

array
(
    a => array ( id = 20, name = chimpanzee )
    b => array ( id = 40, name = meeting )
    c => array ( id = 20, name = dynasty )
    d => array ( id = 50, name = chocolate )
    e => array ( id = 10, name = bananas )
    f => array ( id = 50, name = fantasy )
    g => array ( id = 50, name = football )
)

And I would like to group it into a new array based on the id field in each subarray.

array
(
    10 => array
          (
            e => array ( id = 10, name = bananas )
          )
    20 => array
          (
            a => array ( id = 20, name = chimpanzee )
            c => array ( id = 20, name = dynasty )
          )
    40 => array
          (
            b => array ( id = 40, name = meeting )
          )
    50 => array
          (
            d => array ( id = 50, name = chocolate )
            f => array ( id = 50, name = fantasy )
            g => array ( id = 50, name = football )
          )
)

Thanks!

解决方案
$arr = array();

foreach($old_arr as $key => $item)
{
   $arr[$item['id']][$key] = $item;
}

ksort($arr, SORT_NUMERIC);

这篇关于集团由子阵列值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:46