Compare 2 arrays remove the duplicates that appear in both in php - php

I have two arrays, I want to remove the duplicate(the values that appear in both) And remain with values that are not in Array A. For example I have array Master_arr and Log_arr. I want to remove the duplicates that appear in both and add in master_arr values that dont exist from log_arr. Master_arr contains CAT,DOG,RABBIT and Log_arr contains CAT,DOG,RABBIT,CAR,PHONE.
I want to add CAR and PHONE to Master_arr.
$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
print_r($unique);

try this
$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
$master_arr=array_diff($unique, $master_arr);
print_r($master_arr);

I believe you want
$unique = array_diff(array_merge($master_arr, $log_arr), $master_arr);

Related

How to display data without redundancy

i want to display in my system just one record with the same id.
for example: 01,01,02,02,03,03.
I just want to display 01,02,03.
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
$number = "01,01,02,02,03,03";
$result = implode(',',array_unique(explode(',', $number )));
echo $result; // output 01,02,03
Happy Programming

Put values in array and Get values using foreach

e.g. I have this Product Id values 31,32 from database. I want to put them on array. So, I can use the values for foreach.
What I want to achieve:
I want to get the stock of each product from database according to the given values (31,32).
What I tried:
$product_values = '31,32'; //this values can be different, sample values only
$arr_product_values = array();
$arr_product_values[] = $product_values;
foreach ($arr_product_values as $prod_id) {
echo $prod_id;
}
Expected output:
31 & 32
$arr_product_values[] = $product_values;
That doesnt mean you have a new array with those 2 values now. You just took a comma separated string and assigned it to an array element, that doesnt make it an array itself.
$arr_product_values = array(31,32);
Does.
Now you can loop over it

Inserting two or more arrays into a "Final Array"?

Here is my code:
<?php
//header code to define as json and if $_GET statement...
$JSONArrayA[$variableA] = array('id' => $idA, 'test' => $testVariableA);
$JSONArrayB[$variableB] = array('id' => $idB, 'test' => $testVariableB);
//current code resulting in ["ArrayArray"]
$FinalJSONArray[] = $JSONArrayA . $JSONArrayB;
echo json_encode($FinalJSONArray);
?>
My question: How do I make the array contain two or more arrays? Any help appreciated.
array_merge
$FinalJSONArray = array_merge($JSONArrayA, $JSONArrayB);
Merges the elements of one or more arrays together so that the values
of one are appended to the end of the previous one. It returns the
resulting array.
If you want to instead return an array containing the other two arrays themselves,
use
$FinalJSONArray = array($JSONArrayA, $JSONArrayB);
Try
$FinalJSONArray[] = $JSONArrayA;
$FinalJSONArray[] = $JSONArrayB;
This will reult in 2 sub arrays. If you want them merged use:
$FinalJSONArray[] = $JSONArrayA+$JSONArrayB;
"+" with two arrays unions them (see: http://php.net/manual/en/language.operators.array.php)
Depending on what you want your JSON to look like
$FinalJSONArray = array($JSONArrayA,$JSONArrayB);

Php add duplicate values inside an array

I have an array which contain multiple values inside it, all i want is to find duplicate items and add the number of times they are inside an array. Here goes an example
$someVar = array('John','Nina','Andy','John','Aaron','John','Zack','Kate','Nina');
I want my final result to look like
John = 3;
Nina = 2;
and so on.
EDIT | These values are dynamic i have no idea what these names going to be.
Thanks
Use array_count_values() and array_filter() to achieve this.
$result = array_filter( array_count_values( $someVar), function( $el) {
return $el > 1;
});
$result will be an associative array containing the names as keys and the number of times they occur as values, but only if they were duplicated in the $someVar array.
Here is a demo showing the correct output.

how to compare two arrays and find the count of match elements

i have two arrays i.e$ar1=array("Mobile","shop","software","hardware");and$arr2=arry("shop","Mobile","shop","software","shop")
i want to compare the elements of arr2 to arr1 i.e
foreach($arr2 as $val)
{
if(in_array($val, $arr1))
{
//mycode to insert data into mysql table
variable++; // here there should be a variable that must be increamented when ever match found in $arr2 so that it can be saved into another table.
}
$query="update table set shop='$variable',Mobile='$variable'.......";
}
the $variable should be an integer value so that for later use i can use this variable(shop i.e in this example its value should be 3) to find the match.
My question is how can i get the variable that will increamented each time match found.
Sorry, I don't fully understand the purpose of your code. You can use array_intersect to get common values and array_diff to get the unique values when comparing two arrays.
i want to compare the elements of arr2 to arr1 i.e
Then you are essentially doing the same search for shop three times. It's inefficient. Why not sort and eliminate duplicates first?
Other issues. You are comparing arr2 values with the ones in arr1, which means the number of repetation for "shop" will not be 3 it will be one. Doing the opposite might give you the number of repetation of arr1[1] in arr2 =3.
There are multitude of ways to solve this problem. If efficiency is required,you might wish to sort so you don't have to go beyond a certain point (say s). You can learn to use indexes. Infact the whole datastructure is revolved around these kinds of things - from quick and dirty to efficient.
Not sure I understand the connection between your two arrays. But you can use this to count how many items are in your second array:
$items = array("shop","Mobile","shop","software","shop");
$count = array();
foreach($items as $item)
{
if(isset($count[$item]))
{
$count[$item]++;
}
else
{
$count[$item] = 1;
}
}
print_r($count); // Array ( [shop] => 3 [Mobile] => 1 [software] => 1 )

Categories