remove duplicates in multi array with different keys - php

i know that this question is answered more times here. but i can't figure out how to do this
i have an array with arrays in IT
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
as you see in 4 and 15 keys we have duplicates (94 and 100)
i want to remove all this duplicates except one
so in final to have
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Big thanks

Loop over your outer array, inside loop over your inner arrays.
Check if the current value is already contained in a helper array, if so unset the array element (using the outer and inner level key.)
Then add the current value to the helper array, so that it can be recognized as a duplicate value when it occurs the next time.
$data = [
11 => [3, 4, 5, 37, 41],
4 => [67, 74, 80, 82, 94, 95, 100],
15 => [86, 100, 94]
];
$helperArray = [];
foreach($data as $dkey => $item) {
foreach($item as $ikey => $value) {
if(in_array($value, $helperArray)) {
unset($data[$dkey][$ikey]);
}
$helperArray[] = $value;
}
}
print_r($data);

Here is the most un-elegant solution but it will always do the trick...
<?php
/* Create the array */
$array[11][] = 3;
$array[11][] = 4;
$array[11][] = 5;
$array[11][] = 37;
$array[11][] = 41;
$array[4][] = 67;
$array[4][] = 74;
$array[4][] = 80;
$array[4][] = 82;
$array[4][] = 94;
$array[4][] = 95;
$array[4][] = 100;
$array[15][] = 86;
$array[15][] = 100;
$array[15][] = 94;
/* print the complete array : */
echo '<pre>';
print_r($array);
echo '<pre>';
/* use a simple array to store values as you loop */
$A = [];
foreach($array as $key => $ar){
foreach($ar as $k => $a){
/* if the value already appeared somewhere... remove it from this and next locations...*/
if(in_array($a,$A)){
unset($array[$key][$k]);
}
$A[] = $a;
}
}
/* Print our cleaned array. */
print_r($array);
will return:
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Next time , please share your code, as bad as it can be or whatever it shows your way and that you have tried solving this for real...

I made the data a bit more complex, so that the duplicates are not only in the array elements with the keys 4 and 15.
$data = [
11 => [ 3, 4, 94, 5, 80, 37, 41 ], // will be: 3, 4, 94, 5, 80, 37, 41
4 => [ 67, 74, 80, 82, 95, 100 ], // will be: 67, 74, 82, 95, 100 (80 > key 11)
7 => [ 2, 4, 77, 81, 13, 100 ], // will be: 2, 77, 81, 13 (4 > key 11, 100 > key 4)
15 => [ 86, 100, 94, 13 ] // will be: 86 (100 > key 7, 94 > key 11, 13 > key 7)
];
$keys = array_reverse(array_keys($data));
for ($i = 0; $i < count($keys); $i++) {
$keyI = $keys[$i];
for ($j = $i + 1; $j < count($keys); $j++) {
$keyJ = $keys[$j];
$diff = array_diff($data[$keyI], $data[$keyJ]);
$data[$keyI] = $diff;
}
}
print_r($data);

Related

how to display PHP Multidimensional Arrays?

how to display this using foreach in php?
I want to show it as a list.
$quantity=0;
$quantities = array(
array('Jan',($quantity += $ppmpitem->m1)),
array('Feb',($quantity += $ppmpitem->m2)),
array('Mar',($quantity += $ppmpitem->m3)),
array('Apr',($quantity += $ppmpitem->m4)),
array('May',($quantity += $ppmpitem->m5)),
array('Jun',($quantity += $ppmpitem->m6)),
array('Jul',($quantity += $ppmpitem->m7)),
array('Aug',($quantity += $ppmpitem->m8)),
array('Sep',($quantity += $ppmpitem->m9)),
array('Oct',($quantity += $ppmpitem->m10)),
array('Nov',($quantity += $ppmpitem->m11)),
array('Dec',($quantity += $ppmpitem->m12)),
);
return $quantities;
You have several options, so based on your code (and what I added):
<?php
$quantity=0;
// Added this, so we have an object
$array = [
"m1" => 1,
"m2" => 2,
"m3" => 3,
"m4" => 4,
"m5" => 5,
"m6" => 6,
"m7" => 7,
"m8" => 8,
"m9" => 9,
"m10" => 10,
"m11" => 11,
"m12" => 12
];
// Converted array to object
$ppmpitem = json_decode(json_encode($array));
$quantities = array(
array('Jan',($quantity += $ppmpitem->m1)),
array('Feb',($quantity += $ppmpitem->m2)),
array('Mar',($quantity += $ppmpitem->m3)),
array('Apr',($quantity += $ppmpitem->m4)),
array('May',($quantity += $ppmpitem->m5)),
array('Jun',($quantity += $ppmpitem->m6)),
array('Jul',($quantity += $ppmpitem->m7)),
array('Aug',($quantity += $ppmpitem->m8)),
array('Sep',($quantity += $ppmpitem->m9)),
array('Oct',($quantity += $ppmpitem->m10)),
array('Nov',($quantity += $ppmpitem->m11)),
array('Dec',($quantity += $ppmpitem->m12)),
);
// Pretty print
print("<pre>".print_r($quantities,true)."</pre>");
// As a list where Month is key
foreach($quantities as $key => $value) {
$month = [
$quantities[$key][0] => $quantities[$key][1]
];
$months[$quantities[$key][0]] = $quantities[$key][1];
print_r($month);
}
//Also print full array of months:
print_r($months);
Results are:
Pretty print:
<pre>Array
(
[0] => Array
(
[0] => Jan
[1] => 1
)
[1] => Array
(
[0] => Feb
[1] => 3
)
[2] => Array
(
[0] => Mar
[1] => 6
)
[3] => Array
(
[0] => Apr
[1] => 10
)
[4] => Array
(
[0] => May
[1] => 15
)
[5] => Array
(
[0] => Jun
[1] => 21
)
[6] => Array
(
[0] => Jul
[1] => 28
)
[7] => Array
(
[0] => Aug
[1] => 36
)
[8] => Array
(
[0] => Sep
[1] => 45
)
[9] => Array
(
[0] => Oct
[1] => 55
)
[10] => Array
(
[0] => Nov
[1] => 66
)
[11] => Array
(
[0] => Dec
[1] => 78
)
)
</pre>
Print out "list":
Array
(
[Feb] => 3
)
Array
(
[Mar] => 6
)
Array
(
[Apr] => 10
)
Array
(
[May] => 15
)
Array
(
[Jun] => 21
)
Array
(
[Jul] => 28
)
Array
(
[Aug] => 36
)
Array
(
[Sep] => 45
)
Array
(
[Oct] => 55
)
Array
(
[Nov] => 66
)
Array
(
[Dec] => 78
)
Or print one array (month as key)
Array
(
[Jan] => 1
[Feb] => 3
[Mar] => 6
[Apr] => 10
[May] => 15
[Jun] => 21
[Jul] => 28
[Aug] => 36
[Sep] => 45
[Oct] => 55
[Nov] => 66
[Dec] => 78
)
BR
You may create an array filled from 1 to 12 and apply the array_reduce to get your output.
<?php
class P {
public $m1 = 1;
public $m2 = 1;
public $m3 = 1;
public $m4 = 1;
public $m5 = 1;
public $m6 = 1;
public $m7 = 1;
public $m8 = 1;
public $m9 = 1;
public $m10 = 1;
public $m11 = 1;
public $m12 = 1;
public function __construct() {
}
}
$ppmpitem = new P();
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$quantities = array_reduce($array, function ($carry, $item) use ($ppmpitem) {
$month = DateTime::createFromFormat('!m', $item)->format('M');
$carry['total'] += $ppmpitem->{"m" . $item};
$carry['quantities'][] = [$month, $carry['total']];
return $carry;
}, ['total' => 0, 'quantities' => []])['quantities'];
var_dump($quantities);
There are a number of ways to display a multidimensional array including use of the default PHP's print_r() function. Besides if you want a more precise way of accessing the elements and displaying them you can use a looping criteria such as for() or foreach(). Sample :
foreach($quantities as $qty){
print_r($qty);
}

Get All Array Values have more than 100 or more, and get array all values 100 with less

My scenario is i have an multidimensional array and i want to get separated array from multidimensional array.
Ex:- First array return which is all values have 100 or more that key and value return as a separated and Second if array has one value less than 100 and other values have a 100 and more that array key and value return as a separated see below example,
Array(
[124] => Array
(
[0] => 140
[1] => 101
[2] => 107
[3] => 116
[4] => 100
)
)
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
And output should be like this,
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
Solution with array_filter() and a function filter100(). For explanations of the filter function, see the comments there.
$data = [
124 => [140,101,107,116,100],
164 => [108,111,124,87,278],
162 => [6,79,3],
170 => [6],
171 => [45,34],
172 => [45,134],
];
function filter100($subArr){
$limit = 100;
if(min($subArr) >= $limit) return false; //all values >= 100
$count = count($subArr);
if($count >= 3) return true; //2 values < 100 or 2 values >= 100
//from here on only arrays with 0.1 or 2 values
if($count < 2 OR max($subArr) >= $limit) return false;
return true;
}
$newData = array_filter($data,'filter100');
//test output
echo '<pre>', var_export($newData,true);
Output:
array (
164 =>
array (
0 => 108,
1 => 111,
2 => 124,
3 => 87,
4 => 278,
),
162 =>
array (
0 => 6,
1 => 79,
2 => 3,
),
171 =>
array (
0 => 45,
1 => 34,
),
)
foreach ($total_progress as $key => $value) {
foreach ($value as $key1 => $value1) {
if($value1 > 100 ){
unset($total_progress[$key]);
}
}
}
I have tried with this code but it is removed all the 100 and 100 more values from array and return only 100 less value array. but i want to get array which is 100 and more with 100 less values.
So final output should be like this
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
[170] => Array
(
[0] => 63
[1] => 100
[2] => 45
[3] => 133
[4] => 243
[5] => 170
)
)

Merge Subarray (PHP 5.3.3) [duplicate]

This question already has answers here:
Merge all sub arrays into one [duplicate]
(3 answers)
Closed 2 years ago.
I want to merge all sub arrays of an array in an older project that requires >=5.3.3.
The array mentioned above looks like this:
$array = Array (
Array
(
[0] => 26
[1] => 644
)
Array
(
[0] => 20
[1] => 26
[2] => 644
)
Array
(
[0] => 26
)
Array
(
[0] => 47
)
Array
(
[0] => 47
[1] => 3
[2] => 18
)
Array
(
[0] => 26
[1] => 18
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 8
[5] => 6
[6] => 41
[7] => 31
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 12
[5] => 25
[6] => 41
[7] => 31
)
Array
(
[0] => 198
)
Array
(
[0] => 198
)
Array
(
[0] => 899
))
Now, I'm wondering how I could merge all of those Subrrays into one that looks like this:
Array
(
[1] => 26
[2] => 644
[3] => 20
[4] => 26
[5] => 644
[6] => 26
[7] => 47
[8] => 47
[9] => 3
[10] => 18
[11] => 26
[12] => 18
[13] => 26
[14] => 644
[15] => 24
[16] => 198
[17] => 8
[18] => 6
[19] => 41
[20] => 31
[21] => 26
[22] => 644
[23] => 24
[24] => 198
[25] => 12
[26] => 25
[27] => 41
[28] => 31
[29] => 198
[30] => 198
[31] => 899
)
I know how this could work on a more up to date PHP version. So far on this older version I've tried the following:
print_r(array_merge($array, $emptyArray));
But I get the exact same Array returned.
I also tried something like this:
$result_arr = array();
foreach ($array as $sub_arr) $result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
print_r($result_arr);
Which tells me my second argument is not an array?
I'm a bit confused and hope someone can shed some light on this issue.
this function, I think, will do it well.
function array_flatten(array $array)
{
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
All you have to do is send your array as a parameter of the function, and display the result.
$arrayToFlatt = Array(
Array(26, 644),
Array(20, 26, 644),
Array(26),
Array(47),
Array(47, 3, 18),
Array(26, 18),
Array(26, 644, 24, 198, 8, 6, 41, 31),
Array(26, 644, 24, 198, 12, 25, 41, 31),
Array(198),
Array(198),
Array(899)
);
echo '<pre>';
print_r(array_flatten($arrayToFlatt));
echo '</pre>';
Working since PHP 4
You could use array_walk_recursive(), which will visit each leaf node of the input array, then add this value to an output array...
$output = array();
array_walk_recursive($array, function ($data) use (&$output) {
$output[] = $data;
});
print_r($output);
Your foreach approach is fine, and works for me. But I don't see why you are filtering with array_unique.
<?php
$data =
[
[1,3,5,7],
[2,4,6,8],
[1,2,4,5]
];
$output = [];
foreach ($data as $sub) {
$output = array_merge($output, $sub);
}
var_export($output);
Output:
array (
0 => 1,
1 => 3,
2 => 5,
3 => 7,
4 => 2,
5 => 4,
6 => 6,
7 => 8,
8 => 1,
9 => 2,
10 => 4,
11 => 5,
)
To flatten with the splat (Php 5.6):
$output = array_merge(...$data);
For pre-splat versions:
$output = call_user_func_array('array_merge', $data);
Or array_reduce:
$output = array_reduce($data, 'array_merge', []);
If in doubt foreach:
$output = [];
foreach($data as $v)
foreach($v as $n)
$output[] = $n;
All the above result in the same zero indexed array.

Divide/split the array based on the value suming up to a set value and its relevant element to be grouped inside the splited array

With an array like the one below is there a way so I can divide/split the array based on the value suming up to a set value. e.g if I wanted them to less than equal 250,also its relevant element to be grouped inside the splited array.
Example
Array
(
[42] => 10
[55] => 20
[56] => 10
[57] => 4
[58] => 5
[59] => 240
)
The Output should be as below:
Array
(
[0] => Array
(
['value'] => Array
(
[0] => 10
[1] => 20
[2] => 10
[3] => 4
[4] => 5
),
['element'] => Array
(
[0] => 42
[1] => 55
[2] => 56
[3] => 57
[4] => 58
),
)
[1] => Array
(
['value'] => Array
(
[0] => 240
),
['element'] => Array
(
[0] => 59
),
)
);
Loop the array and keep track of the sum.
When it goes above 250 reset the sum counter and start a new subarray.
$arr = Array(
42 => 10,
55 => 20,
56 => 10,
57 => 4,
58 => 5,
59 => 240
);
// Can we do all in one subarray?
if(array_sum($arr) >= 250){
//No
$sum = 0;
$index = 0;
$result[$index] =[];
//Loop the array and keep the sum in memory
foreach($arr as $key => $val){
if($sum + $val < 250){
//Less than 250, just keep adding the values
$result[$index]['values'][] = $val;
$result[$index]['elements'][] = $key;
$sum += $val;
}else{
// More than 250, create a new subarray and reset sum
$sum = $val;
$index++;
$result[$index] = ['values' => [$val], 'elements' => [$key]];
}
}
}else{
//Yes, group the values and keys
$result = [['values' => array_values($arr), 'elements' => array_keys($arr)]];
}
var_dump($result);
https://3v4l.org/jSdsA

Changing array key value in PHP

I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.
$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);
The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.

Categories