如何通过一个键值合并子阵列并将限定值推入更深层的索引子阵列?

问题描述 投票:1回答:2

我试图基于hash值合并多维数组的子数组。

我的输入如下:

$images_arr = [
    [
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => ['73315_73316_73317_LAKE13437_322.tif'],
        'filepath' => ['_TEST_DATA']
        // and there is more data here
    ],
    [
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => ['73315_73316_73317_LAKE13437_322(1).tif'],
        'filepath' => ['_TEST_DATA/subdirectory']
        // and there is more data here
    ]
];

在一个或多个hash值匹配的情况下,我想合并数组并将filename子阵列值和filepath子阵列值组合到第一个中。

像这样的东西:

array (
  0 => 
  array (
    'hash' => '948e980ed2a36d917f1b3026b04e0016',
    'filename' => 
    array (
      0 => '73315_73316_73317_LAKE13437_322.tif',
      1 => '73315_73316_73317_LAKE13437_322(1).tif',
    ),
    'filepath' => 
    array (
      0 => '_TEST_DATA',
      1 => '_TEST_DATA/subdirectory',
    ),
    // more data here
  ),
)

该数组将包含数千条记录,我需要能够组合多个数组,其中只有hash值匹配。

到目前为止,我的努力是在数组上使用foreach来创建一个变量来缓存哈希值并检查以下循环。然后我能够在这种情况下提取filenamefilepath。不知道这是否是最熟练的方法 - 道歉,我仍然是一个PHP新手。

我的编码尝试:

$cache_hash = null;
foreach ($images_arr as $image) {
    $this_hash = $image['hash'];

    if ($cache_hash != $this_hash) {
        $cache_hash = $this_hash;
    } else {

        foreach ($image['filename'] as $filename) {
            //Get filename
        }

    }
}
php arrays multidimensional-array merge
2个回答
1
投票

通过根据每个数据集的hash值为结果数组分配临时键,您可以确定是否存储了第一次出现的哈希,或者是否需要推送新值。

循环完成后,删除临时(外部)键以生成所需的结果。

代码:(Demo

$array = [
    [
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => ['73315_73316_73317_LAKE13437_322.tif'],
        'filepath' => ['_TEST_DATA']
    ],
    [
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => ['73315_73316_73317_LAKE13437_322(1).tif'],
        'filepath' => ['_TEST_DATA/subdirectory']
    ]
];

foreach ($array as $set) {
    if (!isset($result[$set['hash']])) {          // check if 1st occurrence of hash value
        $result[$set['hash']] = $set;             // save whole set
    } else {                                      // not 1st occurrence
        $result[$set['hash']]['filename'][] = $set['filename'][0];  // push filename value into this hash's group
        $result[$set['hash']]['filepath'][] = $set['filepath'][0];  // push filepath value into this hash's group
    }
}

var_export(array_values($result));  // re-index the result array to remove temp keys

输出:

array (
  0 => 
  array (
    'hash' => '948e980ed2a36d917f1b3026b04e0016',
    'filename' => 
    array (
      0 => '73315_73316_73317_LAKE13437_322.tif',
      1 => '73315_73316_73317_LAKE13437_322(1).tif',
    ),
    'filepath' => 
    array (
      0 => '_TEST_DATA',
      1 => '_TEST_DATA/subdirectory',
    ),
  ),
)

0
投票

您可以使用哈希键准备数组。试试以下。

<?php
// Input array set
$input = array(
    array(
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => array('73315_73316_73317_LAKE13437_322.tif'),
        'filepath' => array('_TEST_DATA')
    ),
    array(
        'hash' => '948e980ed2a36d917f1b3026b04e0016',
        'filename' => array('73315_73316_73317_LAKE13437_322(1).tif'),
        'filepath' => array('_TEST_DATA/subdirectory')
    ),
    array(
        'hash' => '948e980ed2a36d917f1b3026b04e0018',
        'filename' => array('73315_73316_73317_LAKE13437_321.tif'),
        'filepath' => array('_TEST_DATA/subdirectory')
    ),
    array(
        'hash' => '948e980ed2a36d917f1b3026b04e0018',
        'filename' => array('73315_73316_73317_LAKE13437_321(1).tif'),
        'filepath' => array('_TEST_DATA/subdirectory')
    ),
);

$mergeArrayWithHash = [];
foreach ($input as $value) {
    $mergeArrayWithHash[$value['hash']]['hash'] = $value['hash'];
    $mergeArrayWithHash[$value['hash']]['filename'][] = $value['filename'][0];
    $mergeArrayWithHash[$value['hash']]['filepath'][] = $value['filepath'][0];
}
// Your Output array
$result = array_values($mergeArrayWithHash);

echo "<pre>";
print_r($result);
exit;
© www.soinside.com 2019 - 2024. All rights reserved.