This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
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
)
)
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
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
)
)
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>';
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
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
)
)
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',
),
)
Related
I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)
I have an array like this-
Array
(
[sku] => Array
(
[0] => SKU125
[1] => SKU121
[2] => SKU122
[3] => SKU124
)
[variation_description] => Array
(
[0] => test another
[1] => test
[2] => test
[3] => test
)
[price_html] => Array
(
[0] => 400,200
[1] => 500
[2] => 600,300
[3] => 700
)
)
Is it possible to covert the array to like this table-
Any kind of help will be appreciated.
Thanks In Advance
As every key (sku , price_html etc) has same amount of data , so just push the corresponding key data to a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$re_structured = [];
foreach ($data as $each_key_data ) {
foreach ($each_key_data as $key => $value2 ) {
$re_structured[$key][] = $value2;
}
}
var_dump($re_structured);
You can simply iterate through data and create a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$mapped = [];
$keys = array_keys($data);
$rows = count($data[reset($keys)]);
for ($i = 0; $i < $rows; $i++) {
$row = [];
foreach ($keys as $key)
$row[] = $data[$key][$i];
$mapped[] = $row;
}
This will result in
print_r($mapped);
Array
(
[0] => Array
(
[0] => SKU125
[1] => test another
[2] => 400,200
)
[1] => Array
(
[0] => SKU121
[1] => test
[2] => 500
)
[2] => Array
(
[0] => SKU122
[1] => test
[2] => 600,300
)
[3] => Array
(
[0] => SKU124
[1] => test
[2] => 700
)
)
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)
I have the following arrays in PHP:
print_r($employees) =
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[1] => Array
(
[p_id] => T29083388
[name] => Yvan Sergei
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
then this array:
print_r($record) =
Array
(
[0] => Array
(
[c_id] => 1
[section] => 1061
[term] => 201631
[p_id] => T29083388
[c_date] => 2016-04-01 09:14:00
)
)
I want to remove the array element from $employees that matches the p_id of $record. Array $record may have multiple entries like the one shown. If so, any p_id in $record must be removed from $employees.
I tried:
foreach ($employees as $k => $e) {
foreach ($record as $r) {
if ($e['p_id']==$r['p_id']) {
echo "found it!";
// if I uncomment the next line, it crashes! (500 server error)
// unset $employees[$k];
}
}
}
I just want to remove any element from $employees that has any employee that matches any record in $record with that employee id.
You were almost there; just needed parens around your unset()
I also took the liberty to change some of your variable names as single character variable names bother me.
$employees[] = [
'p_id' => 'T29083999',
'name' => 'Robert Plaxo',
];
$employees[] = [
'p_id' => 'T29083388',
'name' => 'Yvan Sergei',
];
$employees[] = [
'p_id' => 'T21083911',
'name' => 'Rhonda Saunders',
];
$employees[] = [
'p_id' => 'H02910382',
'name' => 'Miguel Mercado',
];
$records[] = [
'c_id' => '1',
'section' => '1061',
'term' => '201631',
'p_id' => 'T29083388',
'c_date' => '2016-04-01 09:14:00',
];
foreach ($employees as $key => $employee) {
foreach ($records as $record) {
if ($employee['p_id'] == $record['p_id']) {
echo "found it!";
unset($employees[$key]);
}
}
}
echo "<pre>";
print_r($employees);
Outputs
found it!
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
The short solution using array_column and array_filter functions. It will also fit your requirement "Array $record may have multiple entries":
$p_ids = array_column($record, "p_id");
$employees = array_filter($employees, function($v) use($p_ids){
return !in_array($v["p_id"], $p_ids);
});
print_r($employees);
The output:
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
My array is like this:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord
)
[1] => Array
(
[id] => 2
[name] => b
[hardware_type] => mouse
)
[2] => Array
(
[id] => 1
[name] => a
[hardware_type] => mouse
)
[3] => Array
(
[id] => 1
[name] => a
[hardware_type] => moniter
)
[4] => Array
(
[id] => 2
[name] =>b
[hardware_type] => keyboad
)
)
required out put like this i want only merge hardware type
Array(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord, mouse, moniter
)
[1] => Array
(
[id] => 1
[name] => b
[hardware_type] => keyboard, mouse
)
)
Where $array is the input array you described, where $newarray is the output array you desire, and assuming every value of id has the same name as in your example input:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
if (empty($newarray[$item['id']]['hardware_type']))
$temp[$item['id']]['hardware_type'] = $item['hardware_type'];
else
$temp[$item['id']]['hardware_type'] .= ', ' . $item['hardware_type'];
}
$newarray = array_values($temp);
If you want the hardware_type to be an array instead of a comma-separated list of strings, do this instead:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
$temp[$item['id']]['hardware_type'][] = $item['hardware_type'];
}
$newarray = array_values($temp);