使用3个索引PHP

时间:2017-08-01 17:55:31

标签: php arrays multidimensional-array

我不确定我是否可以正确解释,但我会尝试,

我有一个多维数组,看起来像下面的数组。请注意 GROUPINDEX ELOCATRIND UNITINDEX 中的更改。我尝试了很多循环,合并和过滤,但无法达到预期的效果。

Array
(
[0] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 1
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Date
        [ELOCATRVAL] => 123
        [CODEDESC] => 
    )

[1] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 2
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Decider
        [ELOCATRVAL] => abc
        [CODEDESC] => 
    )

[2] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 5
        [ELOCATRIND] => 1
        [ELOCATRDES] => 
        [ELOCATRVAL] => test-DE
        [CODEDESC] => 
    )

[3] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 1
        [ELOCATRIND] => 2
        [ELOCATRDES] => Grading Date
        [ELOCATRVAL] => 123-2
        [CODEDESC] => 
    )

[4] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 2
        [ELOCATRIND] => 2
        [ELOCATRDES] => Grading Decider
        [ELOCATRVAL] => abc-2
        [CODEDESC] => 
    )

[5] => Array
    (
        [GROUPINDEX] => 2
        [GROUPDESC] => Genre Codes
        [UNITINDEX] => 1
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Date
        [ELOCATRVAL] => 2017-02-01
        [CODEDESC] => 
    )

[6] => Array
    (
        [GROUPINDEX] => 2
        [GROUPDESC] => Genre Codes
        [UNITINDEX] => 2
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Decider
        [ELOCATRVAL] => NGR
        [CODEDESC] => 
    )
)

我需要根据上面提到的索引按层次顺序对它们进行分组。 最终结果应如下所示:

Array
(
[0] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 1
        [ELOCATRIND] => 1, 2
        [ELOCATRDES] => Grading Date:123 | Grading Date : 123-2

    )

[1] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 2
        [ELOCATRIND] => 1, 2
        [ELOCATRDES] => Grading Decider:abc| Grading Decider:abc-2
    )

[2] => Array
    (
        [GROUPINDEX] => 1
        [GROUPDESC] => Grading Parameters
        [UNITINDEX] => 5
        [ELOCATRIND] => 1
        [ELOCATRDES] => 
        [ELOCATRVAL] => test-DE
        [CODEDESC] => 
    )


[5] => Array
    (
        [GROUPINDEX] => 2
        [GROUPDESC] => Genre Codes
        [UNITINDEX] => 1
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Date
        [ELOCATRVAL] => 2017-02-01
        [CODEDESC] => 
    )

[6] => Array
    (
        [GROUPINDEX] => 2
        [GROUPDESC] => Genre Codes
        [UNITINDEX] => 2
        [ELOCATRIND] => 1
        [ELOCATRDES] => Grading Decider
        [ELOCATRVAL] => NGR
        [CODEDESC] => 
    )
)

我在一些消毒之后尝试了以下片段,但它没有用,现在我没有想法。

$result1 = array_map(
    function ($id) use ($Array) {
       return array_map(function ($data) {
            if (is_array($data)) return join(" | ", array_unique($data));
            return $data;
        }, array_reduce(
                array_filter($Array, function ($item) use ($id) { return $id === $item['UNITINDEX']; }),
                function ($result1, $set) { return $result1 = array_filter(array_merge_recursive($result1, $set)); }, [ ]
            )
        );
    },
    array_unique(array_column($Array, 'UNITINDEX'))
);

任何帮助都会很棒,非常感谢。

3 个答案:

答案 0 :(得分:0)

你谈到3个索引,但你根本不把它们当作索引。让我们来使用它们;得到这样的结果怎么样?

第一个索引是GROUPINDEX。

[1] => [
    [GROUPDESC] => Grading Parameters
    [UNITINDEX] => [
        [1] => [
            [ELOCATRIND] => 1, 2
            [ELOCATRDES] => Grading Date: 123 | Grading Date : 123-2
        ]
        [2] => [
            [ELOCATRIND] => 1, 2
            [ELOCATRDES] => Grading Decider: abc | Grading Decider: abc-2
        ]
        [5] => [
            [ELOCATRIND] => 1
            [ELOCATRDES] => 
        ]
    ]
]
[2] => [
        [GROUPDESC] => Genre Codes
        [UNITINDEX] => [
        [1] => [
            [ELOCATRIND] => 1
            [ELOCATRDES] => Grading Date: 2017-02-01
        ]
        [2] => [
            [ELOCATRIND] => 1
            [ELOCATRDES] => Grading Decider: NGR
        ]
    ]
]

获得该功能的功能:

<?php
function parseArr($arr) {
    $return = [];
    foreach($arr as $item) {
        // Check if GROUPINDEX exists
        if(!isset($return[$item['GROUPINDEX']]) {
            $return[$item['GROUPINDEX']] = [
                'GROUPDESC' => $item['GROUPDESC'],
                'UNITINDEX' => []
            ];
        }
        // Check if UNITINDEX exists in this GROUPINDEX
        if(!isset($return[$item['GROUPINDEX']]['UNITINDEX'][$item['UNITINDEX']])) {
            // Create UNITINDEX array
            $return[$item['GROUPINDEX']]['UNITINDEX'][$item['UNITINDEX']] = [
                'ELOCATRIND' => $item['ELOCATRIND'],
                'ELOCATRDES' => $item['ELOCATRDES'] . ': ' . $index['ELOCATRVAL']
            ];
        } else {
            // Update UNITINDEX array, just concat to previous values
            $return[$item['GROUPINDEX']]['UNITINDEX'][$item['UNITINDEX']]['ELOCATRIND'] .= ', ' . $item['ELOCATRIND'];
            $return[$item['GROUPINDEX']]['UNITINDEX'][$item['UNITINDEX']]['ELOCATRDES'] .= ' | ' . $item['ELOCATRDES'] . ': ' . $index['ELOCATRVAL'];
        }
    }
    return $return;
}

答案 1 :(得分:0)

这应该给你一个开始。

<?php
$items = [
    [
        'GROUPINDEX' => 1,
        'GROUPDESC' => 'Grading Parameters',
        'UNITINDEX' => 1,
        'ELOCATRIND' => 1,
        'ELOCATRDES' => 'Grading Date',
        'ELOCATRVAL' => '123',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 1,
        'GROUPDESC' => 'Grading Parameters',
        'UNITINDEX' => 2,
        'ELOCATRIND' => 1,
        'ELOCATRDES' => 'Grading Decider',
        'ELOCATRVAL' => 'abc',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 1,
        'GROUPDESC' => 'Grading Parameters',
        'UNITINDEX' => 5,
        'ELOCATRIND' => 1,
        'ELOCATRDES' => null,
        'ELOCATRVAL' => 'test-DE',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 1,
        'GROUPDESC' => 'Grading Parameters',
        'UNITINDEX' => 1,
        'ELOCATRIND' => 2,
        'ELOCATRDES' => 'Grading Date',
        'ELOCATRVAL' => '123-2',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 1,
        'GROUPDESC' => 'Grading Parameters',
        'UNITINDEX' => 2,
        'ELOCATRIND' => 2,
        'ELOCATRDES' => 'Grading Decider',
        'ELOCATRVAL' => 'abc-2',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 2,
        'GROUPDESC' => 'Genre Codes',
        'UNITINDEX' => 1,
        'ELOCATRIND' => 1,
        'ELOCATRDES' => 'Grading Date',
        'ELOCATRVAL' => '2017-02-01',
        'CODEDESC' => null
    ], [
        'GROUPINDEX' => 2,
        'GROUPDESC' => 'Genre Codes',
        'UNITINDEX' => 2,
        'ELOCATRIND' => 1,
        'ELOCATRDES' => 'Grading Decider',
        'ELOCATRVAL' => 'NGR',
        'CODEDESC' => null
    ]
];

// let create's a dictionary (associative array) keyed on the GROUPINDEX|UNITINDEX
$dict = [];

foreach ($items as $item) {
    $key = $item['GROUPINDEX'] . '|' . $item['UNITINDEX'];
    // check if key does not exist in the dictionary to create a new entry
    if (!isset($dict[$key])) {
        $dict[$key] = [
            'GROUPINDEX' => $item['GROUPINDEX'],
            'GROUPDESC' => $item['GROUPDESC'],
            'UNITINDEX' => $item['UNITINDEX'],
            // for convenience, we'll use arrays here
            'ELOCATRIND' => [],
            'ELOCATRDES' => []
        ];
    }
    // add item to dictionary's entry
    $dict[$key]['ELOCATRIND'][] = $item['ELOCATRIND'];

    // @TODO: ADD SOME CONDITIONAL LOGIC HERE

    $dict[$key]['ELOCATRDES'][] = $item['ELOCATRDES'] . ':' . $item['ELOCATRVAL'];
}

// let's clean up the dictionary now
foreach ($dict as $key => $item) {
    $dict[$key]['ELOCATRIND'] = implode(', ', $item['ELOCATRIND']);
    $dict[$key]['ELOCATRDES'] = implode(' | ', $item['ELOCATRDES']);
}

// sort dictionary by key
ksort($dict);
// remove keys
$dict = array_values($dict);

echo '<pre>' . print_r($dict, true) . '</pre>';
?>

将输出

Array
(
    [0] => Array
        (
            [GROUPINDEX] => 1
            [GROUPDESC] => Grading Parameters
            [UNITINDEX] => 1
            [ELOCATRIND] => 1, 2
            [ELOCATRDES] => Grading Date:123 | Grading Date:123-2
        )

    [1] => Array
        (
            [GROUPINDEX] => 1
            [GROUPDESC] => Grading Parameters
            [UNITINDEX] => 2
            [ELOCATRIND] => 1, 2
            [ELOCATRDES] => Grading Decider:abc | Grading Decider:abc-2
        )

    [2] => Array
        (
            [GROUPINDEX] => 1
            [GROUPDESC] => Grading Parameters
            [UNITINDEX] => 5
            [ELOCATRIND] => 1
            [ELOCATRDES] => :test-DE  
        )

    [3] => Array
        (
            [GROUPINDEX] => 2
            [GROUPDESC] => Genre Codes
            [UNITINDEX] => 1
            [ELOCATRIND] => 1
            [ELOCATRDES] => Grading Date:2017-02-01
        )

    [4] => Array
        (
            [GROUPINDEX] => 2
            [GROUPDESC] => Genre Codes
            [UNITINDEX] => 2
            [ELOCATRIND] => 1
            [ELOCATRDES] => Grading Decider:NGR
        )

)

你会注意到最后三个与你想要的结果不一样。你必须在评论中添加逻辑@TODO(因为我不知道)。

答案 2 :(得分:0)

您的代码尝试非常接近。如果所需的值与原始数组中的每个元素相对应,则array_map()将是一个不错的选择。但是,由于所需输出的原始数组中的每个元素都没有一个元素,因此请在array_reduce()内使用array_filter()而不是array_map()。这样,当使用array_reduce()时,代码可以有条件地将当前项添加到结果数组或更新具有匹配的 UNITINDEX GROUPINDEX 值的现有项。 / p>

在下面的代码段中,还要注意array_keys()用于获取第一个找到的项目的索引(如果存在),array_key_exists()用于检查密钥 ELOCATRVAL 存在于找到的项目上。在这种情况下,该值将附加到 ELOCATRDES 键,并且未设置现有值(使用unset())。

$result1 = array_reduce($Array, function($result, $currentItem) {
    $existingItems = array_filter($result, function ($item) use ($currentItem) { 
        return $currentItem['UNITINDEX'] === $item['UNITINDEX'] && $currentItem['GROUPINDEX'] === $item['GROUPINDEX']; 
    });
    if (count($existingItems)) {//matching item found
        $index = array_keys($existingItems)[0];
        if (array_key_exists('ELOCATRVAL', $result[$index])) {
            $result[$index]['ELOCATRDES'] .= ':'.$result[$index]['ELOCATRVAL'];
            unset($result[$index]['ELOCATRVAL']);
        }
        $result[$index]['ELOCATRIND'] .= ', '.$currentItem['ELOCATRIND'];
        $result[$index]['ELOCATRDES'] .= '| '.$currentItem['ELOCATRDES'].':'.$currentItem['ELOCATRVAL'];
    }
    else { //no match - push the item into the array
        $result[] = $currentItem;
    }
    return $result; //return cumulative array after iterating over each item 
}, []); //pass empty array as initial value of $result

请参阅this PHP playground example中的演示。