dousi2029 2018-11-14 19:20
浏览 33
已采纳

获取多维数组关联列表

I've got the following structue of array, but i need Get a list of mark value that contains the elements elements that are repeated::

this is my Array base $AsocContData[$s_list_100]:

array(5) {
    [01081] => Array(3){
        [id] => 2
        [Mark] => 420
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00358] => Array(3){
        [id] => 6
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00277] => Array(3){
        [id] => 3
        [Mark] => 400
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00357] => Array(3){
        [id] => 1
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
}

What's the best way for get the List?

So the results look like this:

array(3) {
    [0400] => Array(1){
        [count] => 1
        [element] => '00277'
    }
    [0420] => Array(1){
        [count] => 1
        [element] => '01081'
    }
    [0500] => Array(1){
        [count] => 2
        [element] =>'00357,00358'
    }
}

i have this array sorted from:

Sort Array Index by SubArray Value

  • 写回答

1条回答 默认 最新

  • doucheng7234 2018-11-14 20:29
    关注

    Here is the code

    $arr = [
        '01081' => [
        'id' => 2,
        'Mark' => 420,
        'lastUpdated' => '2010-03-17 15:44:12',
        'dataNext' => [
            'more1' => 54,
            'More2' => 54,
        ]
    ],
    '00358' => [
        'id' => 6,
        'Mark' => 500,
        'lastUpdated' => '2010-03-17 15:44:12',
        'dataNext' => [
            'more1' => 54,
            'More2' => 54,
        ]
    ],
    '00277' => [
        'id' => 3,
        'Mark' => 400,
        'lastUpdated' => '2010-03-17 15:44:12',
        'dataNext' => [
            'more1' => 54,
            'More2' => 54,
        ]
    ],
    '00357' => [
        'id' => 1,
        'Mark' => 500,
        'lastUpdated' => '2010-03-17 15:44:12',
        'dataNext' => [
            'more1' => 54,
            'More2' => 54,
        ]
    ]
    ];
    
    $result = [];
    
    foreach($arr as $id => $data) {
    
    if (! isset($result[$data['Mark']])) {
        $result[$data['Mark']] = [
            'count' => 1,
            'element' => $id
        ];
    } else {
        $result[$data['Mark']]['count']++;
        $result[$data['Mark']]['element'] = $result[$data['Mark']]['element'] .','.$id;
        }
    
    }
    ksort($result);
    print_r($result);
    

    Screenshot of programs output

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题