在PHP的多维数组中组合重复键

时间:2018-07-24 21:15:09

标签: php

这是PHP代码中的外观:

<?php
$array = array(
    array(
        'name'  => 'filter_amount',
        'value' => '100-ml'
    ),
    array(
        'name'  => 'filter_amount',
        'value' => '200-ml'
    ),
    array(
        'name'  => 'page_size',
        'value' => '7'
    )
);
print_r($array);
?>

print_r()函数输出的示例:

Array
(
    [0] => Array
        (
            [name] => filter_amount
            [value] => 100-ml
        )

    [1] => Array
        (
            [name] => filter_amount
            [value] => 200-ml
        )

    [2] => Array
        (
            [name] => page_size
            [value] => 7
        )
)

我需要合并数组中 filter_amount 值的重复项。 这些重复项的值必须用逗号分隔,并且结果应为以下代码:

Array
(
    [0] => Array
        (
            [name] => filter_amount
            [value] => 100-ml,200-ml
        )

    [1] => Array
        (
            [name] => page_size
            [value] => 7
        )

    [2] => Array
        (
            [name] => orderby
            [value] => rating
        )

    [3] => Array
        (
            [name] => paged
            [value] => 1
        )
)

6 个答案:

答案 0 :(得分:4)

由于您希望value用逗号连接,因此必须对其进行循环处理

<?php

//Allow me to change this variable name, just to not create confusion
$content = array(
    array(
        'name'  => 'filter_amount',
        'value' => '100-ml'
    ),
    array(
        'name'  => 'filter_amount',
        'value' => '200-ml'
    ),
    array(
        'name'  => 'page_size',
        'value' => '7'
    )
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
  //Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array 
  //consisting in key => value where the key is 'name' and equals 
  //what we look for that is(filter_ammount)?
  $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
  //If not, let's place this array in the $output array
  if ($key === false) {
      array_push($outputArray, $innerArray);
  } else { 
      //If exists, then $key is the $key of the $outputArray and let's add to its value 
      //our current value, that is in our $innerArray, concatenated with a comma
      $outputArray[$key]['value'] .= ",". $innerArray['value'];
  }
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name 
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>

参考

  

http://php.net/manual/en/function.array-search.php

     

http://php.net/manual/en/function.array-column.php

答案 1 :(得分:1)

如何通过2个重复的列合并两项?

[root @ localhost TEST]#php R00.php 数组 ( [0] =>数组 ( [0] => S01 [1] => 172.16.20.222 [2] => 10.10.10.100 [3] => 445 )

[1] => Array
    (
        [0] => S02
        [1] => 10.10.10.10
        [2] => 192.168.100.100
        [3] => 22
    )

[2] => Array
    (
        [0] => S03
        [1] => 10.10.10.10
        [2] => 192.168.100.100
        [3] => 22
    )

[3] => Array
    (
        [0] => S04
        [1] => 172.16.20.222
        [2] => 10.10.10.100
        [3] => 23
    )

[4] => Array
    (
        [0] => S05
        [1] => 100.100.100.100
        [2] => 192.168.100.100
        [3] => 22
    )

[5] => Array
    (
        [0] => S06
        [1] => 192.168.200.10
        [2] => 192.168.100.100
        [3] => 22
    )

[6] => Array
    (
        [0] => S07
        [1] => 10.10.10.10
        [2] => 192.168.100.100
        [3] => 22
    )

[7] => Array
    (
        [0] => S08
        [1] => 192.168.100.100
        [2] => 10.10.100.106
        [3] => 446
    )

[8] => Array
    (
        [0] => S09
        [1] => 172.16.20.223
        [2] => 10.10.10.108
        [3] => 447
    )

[9] => Array
    (
        [0] => S10
        [1] => 192.168.100.100
        [2] => 10.10.10.109
        [3] => 448
    )

) [root @ localhost TEST]#

将1或2个项目乘以2列重复,是我需要的结果

数组 ( [0] =>数组 ( [0] => S01,S04 [1] => 172.16.20.222 [2] => 10.10.10.100 [3] => 445,23 )

[1] => Array
    (
        [0] => S02 , S03 , S07
        [1] => 10.10.10.10
        [2] => 192.168.100.100
        [3] => 22
    )

[3] => Array
    (
        [0] => S05 , S06
        [1] => 100.100.100.100 , 192.168.200.10
        [2] => 192.168.100.100
        [3] => 22
    )

[4] => Array
    (
        [0] => S08
        [1] => 192.168.100.100
        [2] => 10.10.100.106
        [3] => 446
    )

[5] => Array
    (
        [0] => S09
        [1] => 172.16.20.223
        [2] => 10.10.10.108
        [3] => 447
    )

[6] => Array
    (
        [0] => S10
        [1] => 192.168.100.100
        [2] => 10.10.10.109
        [3] => 448
    )

答案 2 :(得分:0)

尝试一下:

<?php
$array = array(
    array(
        'name'  => 'filter_amount',
        'value' => '100-ml'
    ),
    array(
        'name'  => 'filter_amount',
        'value' => '200-ml'
    ),
    array(
        'name'  => 'page_size',
        'value' => '7'
    )
);

$tmp = array();

foreach($array as $val) {
    $tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
    $item = implode(',', array_unique(explode(',', implode(',',$v['values'])))); 
    $newArr[] = array('name' => $k, 'value' => $item);
}

echo '<pre>';
print_r($newArr);
echo '</pre>';

答案 3 :(得分:0)

通过以下疯狂的混乱获得它:

$name = array_column($array, 'name');
$value = array_column($array, 'value');

foreach($name as $nk=>$nv)
    foreach($value as $vk=>$vv)
        if($nk == $vk)
            $a[$nv][] = $vv;

foreach($a as $k=>$v)
    $b[$k] = implode(',', $v);


$z = 0;
foreach($b as $k=>$v)
{
    $c[$z]['name'] = $k;
    $c[$z]['value'] = $v;
    $z++;
}

$ c是结果数组

答案 4 :(得分:0)

或使用混合函数数组:

<?php
$array = array(
    array(
        'name'  => 'filter_amount',
        'value' => '100-ml'
    ),
    array(
        'name'  => 'filter_amount',
        'value' => '200-ml'
    ),
    array(
        'name'  => 'page_size',
        'value' => '7'
    )
);

$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];

foreach (array_unique($names) as $k)
    $result[$k] = implode(", ", array_filter($values,
            function($v, $indx) use ($names, $k) {
                return $names[$indx] == $k;
            }, ARRAY_FILTER_USE_BOTH));

print_r($result);

$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];

print_r($result2);

结果:

Array
(
    [filter_amount] => 100-ml, 200-ml
    [page_size] => 7
)
Array
(
    [0] => Array
        (
            [name] => filter_amount
            [value] => 100-ml, 200-ml
        )

    [1] => Array
        (
            [name] => page_size
            [value] => 7
        )

)

答案 5 :(得分:0)

到目前为止,所有其他答案都使用两种或多种迭代技术来完成此任务。只需一个循环。

在迭代时,基于name值构建关联的输出数组。如果未设置关联键,则保存整个行。如果已设置,则只需将逗号然后将新的value数据附加到存储的value元素中即可。

使用临时密钥可以使isset()迅速检查是否存在。由于php如何将数组(作为哈希映射)对待,因此它将始终胜过array_search()in_array()

在循环结束时通过调用array_values()删除临时键。

代码:(Demo

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

输出:

array (
  0 => 
  array (
    'name' => 'filter_amount',
    'value' => '100-ml,200-ml',
  ),
  1 => 
  array (
    'name' => 'page_size',
    'value' => '7',
  ),
)