php 按列值对2d数组进行分组,并连接每个组内的值

inn6fuwd  于 2022-12-21  发布在  PHP
关注(0)|答案(4)|浏览(121)

我有一个简单的位置邮政编码和他们的名字的PHP数组。我想压缩'名称'的代码。这个代码来自WooCommerce数据库区域。

$new_arr = [
    [
        'name' => 'Jambi Selatan',
        'code' => '36139',
        'code_name' => '36139 - Jambi Selatan'
    ],
    [
        'name' => 'Jambi Selatan',
        'code' => '36137',
        'code_name' => '36137 - Jambi Selatan'
    ],
    [
        'name' => 'Bagan Pete',
        'code' => '36129',
        'code_name' => '36129 - Bagan Pete'
    ],
    [
        'name' => 'Bagan Pete',
        'code' => '36127',
        'code_name' => '36127 - Bagan Pete'
    ]
];

我想得到的最终结果由'名称'和'代码'组合如下:我尝试了array_unique方法,但不起作用。

Array (
    [0] => Array
        (
            [name] => Jambi Selatan
            [code] => 36139, 36137
            [code_name] => 36139, 36139 - Jambi Selatan
        )

    [1] => Array
        (
            [name] => Bagan Pete
            [code] => 36127, 36129
            [code_name] => 36127, 36129 - Bagan Pete
        )
)

我尝试了此方法,但未在“code_name”处修复

$out = array();
foreach ($new_arr as $key => $value){
    if (array_key_exists($value['name'], $out)){
        $out[$value['name']]['code'] .= ', '.$value['code'];
    } else {
        $out[$value['name']] = array(
            'name' => $value['name'],
            'code' => $value['code'],
            'code_name' => $value['code'] . ' - ' . $value['name']
        );
    }
}
$out = array_values($out);

打印_r($输出);

rbpvctlc

rbpvctlc1#

您必须按in_array检查重复名称并更新现有数组值。如果不存在,请将该值插入$out数组。

$out = array();
foreach($new_arr as $k=>$v) {
    //empty array state
    if(count($out) == 0) {
        $out[] = $v;
        continue;
    }   
    foreach ($out as $key => $value) {
        if(in_array($v["name"],$value)) {
            $out[$key]["code"] .= ",".$v["code"];
            //for the code_name output as OP described
            $nn = explode("-", $value["code_name"]);
            $l = count($nn) - 1;
            unset($nn[$l]);
            $out[$key]["code_name"] = implode($nn).",".$v["code_name"];
            break;
        } else {
            if((count($out)-1) == $key) {
                $out[] = $v;
            }
        }
    }
}
var_dump($out);
wpx232ag

wpx232ag2#

对于像我这样有问题的人,这个方法来解决它:

$out = array();
            foreach ($new_arr as $key => $value){
                if (array_key_exists($value['name'], $out)){
                    $out[$value['name']]['code'] .= ', '.$value['code'];
                    $out[$value['name']]['code_name'] .= ', '.$value['code'] . ' - ' . $value['name'];
                } else {
                    $out[$value['name']] = array(
                        'name' => $value['name'],
                        'code' => $value['code'],
                        'code_name' => $value['code']
                    );
                }
            }
            $out = array_values($out);
            print_r($out);

最终结果;

Array
(
    [0] => Array
        (
            [name] => Jambi Selatan
            [code] => 36139, 36137
            [code_name] => 36139, 36137 - Jambi Selatan
        )

    [1] => Array
        (
            [name] => Bagan Pete
            [code] => 36129, 36127
            [code_name] => 36129, 36127 - Bagan Pete
        )

)
44u64gxh

44u64gxh3#

请尝试以下方法之一:

<?php
$arr = Array (
    Array
        (
            'name' => 'Jambi Selatan',
            'code' => '36139',
            'code_name' => '36139 - Jambi Selatan'
        ),
    Array
        (
            'name' => 'Jambi Selatan',
            'code' => '36137',
            'code_name' => '36137 - Jambi Selatan'
        ),
    Array
        (
            'name' => 'Bagan Pete',
            'code' => '36129',
            'code_name' => '36129 - Bagan Pete'
        ),
    Array
        (
            'name' => 'Bagan Pete',
            'code' => '36127',
            'code_name' => '36127 - Bagan Pete'
        )
);

$newarr = array();
$finalArr = array();
foreach($arr as $aa) {
    $newarr[$aa['name']][] = $aa;
}

foreach($newarr as $kk => $bb) {
    foreach($bb as $cc) {
        $finalArr[$kk]['name'] = $cc['name'];

        if(isset($finalArr[$kk]['code'])) {
            $finalArr[$kk]['code'] = $finalArr[$kk]['code'].','.$cc['code'];
        } else {
            $finalArr[$kk]['code'] = $cc['code'];
        }
        if(isset($finalArr[$kk]['code_name'])) {
            $finalArr[$kk]['code_name'] = $finalArr[$kk]['code_name'].','.$cc['code_name'];
        } else {
            $finalArr[$kk]['code_name'] = $cc['code_name'];
        }
    }
}
echo "<pre>";
print_r($finalArr);
echo "</pre>";
?>
mnemlml8

mnemlml84#

一定要避免任何使用多个循环来分组和连接数据的建议。
我赞同@Optional的代码片段,另一种方法是将引用变量推入结果数组,然后只将逗号分隔的值连接到适当的引用变量。
代码:(Demo

$result = [];
foreach ($arr as $row) {
    if (!isset($ref[$row['name']])) {
        $ref[$row['name']] = $row;
        $result[] = &$ref[$row['name']];
    } else {
        $ref[$row['name']]['code'] .= ', ' . $row['code'];
        $ref[$row['name']]['code_name'] .= ', ' . $row['code_name'];
    }
}
var_export($result);

对于任何坚持销毁引用的纯粹开发人员来说,在循环之后调用unset($ref)
以下是@Optional的代码片段的简化版本:(Demo

$result = [];
foreach ($arr as $row) {
    if (!isset($result[$row['name']])) {
        $result[$row['name']] = $row;
    } else {
        $result[$row['name']]['code'] .= ', ' . $row['code'];
        $result[$row['name']]['code_name'] .= ', ' . $row['code_name'];
    }
}
var_export(array_values($result));

相关问题