素材牛VIP会员
数组根据值 如何进行合并处理
 间***p  分类:PHP代码  人气:853  回帖:2  发布于6年前 收藏

问题

数组1如何 可以组合成数组2的形式?

数组1

$a = [
    [
        'tagId' => '43',
        'streetName' => '锦春路',
        'placecode'  =>  '010170'
    ],
    [
        'tagId' => '44',
        'streetName' => '滨湖路',
        'placecode'  =>  '010203'
    ],
    [
        'tagId' => '44',
        'streetName' => '滨湖路',
        'placecode'  =>  '010204'

    ]
];
打印出结果
array (size=3)
  0 => 
    array (size=3)
      'tagId' => string '43' (length=2)
      'streetName' => string '锦春路' (length=9)
      'placecode' => string '010170' (length=6)
  1 => 
    array (size=3)
      'tagId' => string '44' (length=2)
      'streetName' => string '滨湖路' (length=9)
      'placecode' => string '010203' (length=6)
  2 => 
    array (size=3)
      'tagId' => string '44' (length=2)
      'streetName' => string '滨湖路' (length=9)
      'placecode' => string '010204' (length=6)

数组2

$b = [
    [
        'tagId' => '43',
        'streetName' => '锦春路',
        'placeCodes'  =>  ['010170']
    ],
    [
        'tagId' => '44',
        'streetName' => '滨湖路',
        'placeCodes'  =>  ['010203','010204']
    ]
];
打印出结果
array (size=2)
  0 => 
    array (size=3)
      'tagId' => int 43
      'streetName' => string '锦春路' (length=9)
      'placeCodes' => 
        array (size=1)
          0 => string '010170' (length=6)
  1 => 
    array (size=3)
      'tagId' => int 44
      'streetName' => string '滨湖路' (length=9)
      'placeCodes' => 
        array (size=2)
          0 => string '010203' (length=6)
          1 => string '010204' (length=6)
 标签:php

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv6 码匠
hx***lf JAVA开发工程师 6年前#1

$arr = [
    0 => [
        'tagId' => '43',
        'streetName' => '锦春路',
        'placecode' => '010171'
    ],
    1 => [
        'tagId' => '44',
        'streetName' => '滨湖路',
        'placecode' => '010173'
    ],
    2 => [
        'tagId' => '43',
        'streetName' => '锦春路',
        'placecode' => '010172'
    ],
    3 => [
        'tagId' => '44',
        'streetName' => '滨湖路',
        'placecode' => '010174'
    ],
];


$new_arr = [];
$tmp_key = [];
$i       = 0;
foreach ($arr as $key => $value) {
    if(in_array($value['tagId'] . $value['streetName'], $tmp_key) == FALSE) {
        $tmp_key[$i] = $value['tagId'] . $value['streetName'];
        $new_arr[$i] =  [
            'tagId'      => $value['tagId'],
            'streetName' => $value['streetName'],
            'placecode'  => [$value['placecode']]
        ];
        $i++;
    }else {
        $key = array_search($value['tagId'] . $value['streetName'], $tmp_key);
        if(in_array($value['placecode'], $new_arr[$key]['placecode']) == FALSE) {
            $new_arr[$key]['placecode'][] = $value['placecode'];
        }
    }
}

var_dump($arr);
var_dump($new_arr);
Lv1 新人
凌***志 移动开发工程师 6年前#2

为何不直接使用 tagId 作为 key?
这样子就array_key_exists一判断就可以了,已经添加到新数组的直接只添加 placecode字段就可,还没有添加过就直接添加整个到新数组。

另外,就是最后的结果你不想使用 tagId 作为 key,那把array_values把 key 去掉就得到了。

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取