Unset one duplicate value in array PHP - php

I have 2 arrays: Array1 and Array2. As you can see in Array1 I have 2 duplicate values. So what I want to do, is unset one of dublicates (doesn't matter which one) and as a result I need to unset value from Array2 with the same key as already unset value in Array1
Array1
(
[0] => 1331-14-2-45
[1] => 1344-1-4-22
**[2] => 1409-1-1-4**
[4] => 1312-14-1-23
**[5] => 1409-1-1-4**
[6] => 1365-10-3-30
)
AND
Array2
(
[0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[4] => deviceNotActive#nemodel.GPON.4.6
[5] => deviceNotActive#nemodel.GPON.4.6
[6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)

<?php
$array1 = array
(
0 => '1331-14-2-45',
1 => '1344-1-4-22',
2 => '1409-1-1-4',
4 => '1312-14-1-23',
5 => '1409-1-1-4',
6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
4 => 'deviceNotActive#nemodel.GPON.4.6',
5 => 'deviceNotActive#nemodel.GPON.4.6',
6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
unset($array2[$v]);
}

You can use the array_unique() function, and maybe take a look at the Array functions list.

Use array_unique function and array_diff
$uniqueValues = array_unique($inputArray); //your first array
$deletedValues = array_diff($inputArray, $uniqueValues);
foreach($deletedValues as $key => $deletedValue){
unset($secondIput[$key]); //you second array
}

Related

How to remove same value from an array

I have this array. I want to duplicate all records form an array. I tried array_unique but it's removing duplicate but doesn't remove orignal value.
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 1
[4] => 6
[5] => 1
[6] => 23
[7] => 2
)
I want to remove all duplicate value like 1 and 2 and I want this output :
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You could use a combination of array_filter and array_count_values.
$values = [1,2,3,1,6,1,23,2];
$result = array_filter(array_count_values($values), function($x) {
return $x === 1;
});
print_r(array_keys($result));
Result:
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You can also use array_intersect with array_count_values.
Array_intersect returns values that is 1, and array_keys returns the keys (values).
$values = [1,2,3,1,6,1,23,2];
$result = array_keys(array_intersect(array_count_values($values), [1]));
var_dump($result); //[3,6,23]
https://3v4l.org/cHU5E
Another option is to use array_unique and the use array_diff_assoc() to get a list of what has been removed.
Using that array list in array_diff results in the values that is not duplicated.
$values = [1,2,3,1,6,1,23,2];
$diff = array_diff_assoc($values, array_unique($values));
$result = array_diff($values, $diff);
var_dump($result); //[3,6,23]
https://3v4l.org/XM5sk

Group rows by one column and populate subarray within group with another column's value

I have an array coming from my database and simply it consists of questions and answers. I am trying to merge 2 arrays and create multidimensional array if values are more than one.
Array
(
[0] => Array
(
[question_id] => 1
[option_id] => 1
)
[1] => Array
(
[question_id] => 2
[option_id] => 3
)
[2] => Array
(
[question_id] => 3
[option_id] => 5
)
[3] => Array
(
[question_id] => 3
[option_id] => 6
)
)
I've tried to separate answers and questions to 2 different arrays but couldn't figure how to merge them again.
$user_questions = array_column($answers, 'question_id');
$user_answers = array_column($answers, 'option_id');
What I need is (question 3 has 2 answers) :
Array
(
[1] => 1
[2] => 3
[3] => Array (5, 6)
)
You can group your data like this as you fetch the results from your query instead of processing it after the fact. To get the array you have now, you're currently doing something like this:
while ($row = $stmt->someFetchMethod()) {
$result[] = $row;
}
Instead, use the question id as the key in your result array, and append the option id to an array at that key.
while ($row = $stmt->someFetchMethod()) {
$result[$row['question_id']][] = $row['option_id'];
}
Below code will create a new array by looping the existing array.
// Considering your existing array to be like this
$array = array(
'0' => array('question_id' => 1,'option_id' => 1),
'1' => array('question_id' => 2, 'option_id' => 3 ),
'2' => array('question_id' => 3,'option_id' => 5),
'3' => array('question_id' => 3,'option_id' => 6)
);
//define new array
$new_array = array();
// loop the array
foreach($array as $key=>$value){
// if the option/answer is already set to to question key
if(isset($new_array[$value['question_id']])){
// if question key is an array, push new option to that array
if(is_array($new_array[$value['question_id']])){
array_push($new_array[$value['question_id']], $value['option_id']);
}else{
// convert question key to array with the old value and new option value
$new_array[$value['question_id']] = array($new_array[$value['question_id']],$value['option_id']);
}
}
else{
// assing option as value to question key
$new_array[$value['question_id']] = $value['option_id'];
}
}
print_r($new_array);
Out put:
Array
(
[1] => 1
[2] => 3
[3] => Array
(
[0] => 5
[1] => 6
)
)

merge/sum multi dimentional array php

I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).
When the values are integers, I need to sum them in the final array.
When the values are arrays, I need to loop through the values and sum them in the final array.
If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)
My arrays are like this:
ARRAY 1
[1466859600] => Array
(
[TOTAL] => 27217
[AAA] => Array
(
[FD_CDP] => 1746
[LO_SC_MIC] => 4654
[FD_ATS] => 893
[CDP] => 40
[SUPERVISION] => 9
[CONTROL] => 4
[ATS] => 4
[EVT_ACK] => 3
)
[BBB] => Array
(
[FD_CDP] => 1376
[LO_SC_MIC] => 4606
[FD_ATS] => 826
[FD_ATSS] => 451
[LO_SFRC] => 4
[FD_S2] => 259
[2_LOSC] => 2
)
[CCC] => Array
(
[FD_CDP] => 1333
[LO_SC_MIC] => 4725
[FD_ATS] => 856
[CONTROL] => 4
[ATS] => 2
[EVT_ACK] => 5
)
ARRAY 2
[1466859600] => Array
(
[TOTAL] => 95406
[AAA] => Array
(
[FD_ATSS] => 1719
[LO_SC_MIC] => 16830
[CONTROL] => 16
[NEW] => 7
[NOEL] => 206
)
[BBB] => Array
(
[SUPERVISION] => 23
[CDP] => 158
[CONTROL] => 40
[2_LOSC] => 14
[ATS] => 6
[EVT_ACK] => 4
)
[CCC] => Array
(
[EVT_ACK] => 167
[LO_SFRC] => 248
[SUPERVISION] => 23
)
I wrote a function like this :
function sumArrayValues($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if (is_array($array1[$key]))
{
echo "it's an array\n I need to reloop\n";
sumArrayValues($array1[$key], $array2[$key]);
}
else
{
echo "FIRST VALUE TO SUM\n";
print_r($array1[$key]."\n");
echo "SECOND VALUE TO SUM\n";
print_r($array2[$key]."\n");
$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
echo "--------RESULT of SUM array1&2----------\n";
}
}
return $array1;
}
But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.
A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that.
Any ideas ?
You can get all the keys for the foreach loop, live demo.
Note, you also can check if a key of any array is undefined, then save the defined value for the key.
function sumArrayValues($array1, $array2)
{
$keys = array_keys($array1 + $array2);
foreach ($keys as $key)
{
if (is_array($array1[$key]) || is_array($array2[$key]))
$array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
else
#$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
}
return $array1;
}

error deleting sub-array based on key-value pair

I'm trying to delete an array where the key is [ITEM_ID] searching the value 4
I would need to remove the whole array, but I cannot do it.
Array
(
[0] => Array
(
[ITEM_ID] => 4
[ITEM_MODEL] => BASIC Armario
[ITEM_FABRICANTE] => 1
[ITEM_COLOR] => Wenge
[ITEM_QUANTITY] => 1
)
[1] => Array
(
[ITEM_ID] => 8
[ITEM_MODEL] => Armario 2 Puertas
[ITEM_FABRICANTE] => 1
[ITEM_COLOR] => Roble
[ITEM_QUANTITY] => 1
)
)
I'm trying with this code:
$array = array("ITEM_ID" => "4");
print_r($array);
unset($array['ITEM_ID']);
print_r($array);
You will have to loop through the array and unset the proper sub-array:
// begin looping
foreach($array as $key=>$value)
{
// check if ITEM_ID is 4
if($value['ITEM_ID'] == '4')
{
// unset the array item using the $key
unset($array[$key]);
// stop the loop
break;
}
}

compare multi-dimensional arrays and return they keys who's values are different in php from the first array

I have two multi-dimensional array I want to take only those array whose
key values are different from the first array
Here is my two array:
$array1 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => DDC
[job_type] => Manger
[job_title] => Manager
[job_appointment_date] => 2013-07-17
[job_duration] => one year
)
)
and this is my second array
$array2 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => ouo
[job_type] => uououo
[job_title] => udds
[job_appointment_date] => 2013-07-17
[job_duration] => uo
)
);
I tried array_diff and array_diff_assoc it also not worked for me
i get this error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: history/home.php
Line Number: 729
Something like this should get you there, depending on what exactly you want:
$diff = array_udiff($array1, $array2, function (array $a, array $b) {
return (int)array_diff($a, $b);
});
Adjust the comparison function to compare what exactly you want to compare.
http://php.net/array_udiff
foreach, array_unique and possibly array_udiff should help you here.
PHP Manual:
Unique Arrays
Data Comparison
For Each
For a simple array:
$result = array_unique($array);
In your case there's a function from PHP Manual for this:
<?php
function specified_array_unique($array, $value)
{
$count = 0;
foreach($array as $array_key => $array_value)
{
if ( ($count > 0) && ($array_value == $value) )
{
unset($array[$array_key]);
}
if ($array_value == $value) $count++;
}
return array_filter($array);
}
?>
There's been a post that is similar to what you're asking;
Stack Overflow - array_udiff

Categories