0

here's how it looks in the PHP code:

<?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);
?>

Example of print_r() function output:

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

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

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

I need to combine duplicates of filter_amount values from the array. The values of these duplicates must be commas separated and the result should be the following code:

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
        )
)
  • What kind of techniques in code have you tried so far? where does `paged` come from? – Scuzzy Jul 24 '18 at 21:26
  • This exact question was asked hours ago, did you delete it? – AbraCadaver Jul 24 '18 at 21:27
  • I'm pretty sure this same question was marked as a duplicate already. – Kisaragi Jul 24 '18 at 21:28
  • Being on another exchange site, moderators probably didn't know and just moved it here. – Scuzzy Jul 24 '18 at 21:29
  • Why do you focus on `print_r()`? It is a function only used basically for human troubleshooting. What is your **real** problem here? If you just want to print a given structure in your prefered format just do the code that walks the structure and do the prints you want it to do – Patrick Mevzek Jul 24 '18 at 21:32

6 Answers6

4

Since you want value to be concatenated by a comma, you'll have to make a cycle of it

<?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'));
?>

References

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

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

Community
  • 1
  • 1
abr
  • 2,071
  • 22
  • 38
1

How to combine two items by 2 duplicate columns?

[root@localhost TEST]# php R00.php Array ( [0] => Array ( [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]#

combine 1 or 2 items by 2 column duplicate below is result I need

Array ( [0] => Array ( [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
    )

)

0

Try this:

<?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>';
Fobos
  • 1,076
  • 8
  • 18
0

got it with the following crazy mess:

$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 is the resulting array

Leo Tahk
  • 420
  • 1
  • 6
  • 15
0

Or using a medley of array functions:

<?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);

Results in:

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
        )

)
wordragon
  • 1,297
  • 9
  • 16
0

All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.

Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.

Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).

Remove the temporary keys when the loop is finished by calling array_values().

Code: (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));

Output:

array (
  0 => 
  array (
    'name' => 'filter_amount',
    'value' => '100-ml,200-ml',
  ),
  1 => 
  array (
    'name' => 'page_size',
    'value' => '7',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136