获取与多维数组关联的列表

时间:2018-11-14 19:20:33

标签: php arrays count

我具有以下数组结构,但是我需要获取一个标记值列表,其中包含重复的元素元素:

这是我的数组基础$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
        }
    }
}

获取列表的最佳方法是什么?

结果如下:

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

我将此数组排序为:

Sort Array Index by SubArray Value

1 个答案:

答案 0 :(得分:0)

这是代码

$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