In PHP,array concept - php

I have two arrays...I want to find the common element in the array.I want to find the unique values in array1 and unique values in array2....
Example:
Array1:"red,blue,green,violet"
Array2:"yellow,orange,violet,blue
Now i want to know how will i retrive
uniq_Arr1=>red,green
uniq_Arr2=>yellow,orange
common_Arr=>violet,blue
That is,From each array it sholud retrive the unique elements and the common elements...
Please guide me to know this...

Use array_intersect() to find common elements and array_diff() to find the differences.
Here's my test code:
$array1 = array("red", "blue", "green", "violet");
$array2 = array("yellow", "orange", "violet", "blue");
$uniq_arr1 = array_diff($array1, $array2);
$uniq_arr2 = array_diff($array2, $array1);
$common_arr = array_intersect($array1, $array2);

Related

Say how many items are the same in 2 arrays?

Is there a way within PHP and MySQL to be able to compare 2 different array (list) variables and say how many items are the same
For example,
$array1 = "hello, bye, google, laptop, yes";
$array2 = "google, bye, windows, no, phone";
Then an echo statement would say how many items are the same. In this example, it would be 2 and this would be echoed.
This is different to most array questions because of the way my site is set up using commas which can make it quite complicated
Try array_intersect() function in php
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r(count($result));
?>
You can count elements in the output array using count() function,
doc link
$array_new1 = explode(',',$array1);
$array_new2 = explode(',',$array2);
$array_1 = array_map('trim', $array_new1);
$array_2 = array_map('trim', $array_new2);
$data =array();
foreach($array_2 as $value){
if(in_array($value,$array_1)){
$data[] = $value;
}
}
echo count($data);
First you have to convert your string to array with trim. Then use
array_intersect to get common values.
$array1 = "hello, bye, google, laptop, yes";
$array2 = "google, bye, windows, no, phone";
$array_new1 = array_map('trim', explode(',', $array1));
$array_new2 =array_map('trim', explode(',', $array2));
$common = array_intersect($array_new1, $array_new2);
print_r($common);
echo count($common);

How to merge, combine associative array with sequential array in php?

How do I combine these two arrays into a single array say $array3?
$array1 =[1,2,3];
$array2=['a'=>1,'b'=>2,'c'=>3];
Try this
$temp = array_slice(array_keys($array1), 0, count($array2));
$array3 = array_merge($array1, array_combine($temp, $array2));
I believe this is what you're asking.

How to get unique value from array in PHP?

I have two array inputs like this :
$array1 = [1,2,3,4,6];
$array2 = [1,3];
$output = array_merge(array_diff($array1,$array2),array_diff($array2,$array1));
Now I want to check array1 with array 2 and eliminate 1 and 3 in $array1
and the output I am expecting is
$output = [2,4,6];
but in this method I get some bugs, when array2 have single value e.g.: $array2 = [1]; , $array1 = [1,2,3,4,6]; the output should be $output = [2,3,4,6];. But I am getting $array1 all values [1,2,3,4,6];
Simple :)
(Just un-complicate your code and you don't need anything new for that)
<?php
$array1 = array(1,2,3,4,6);
$array2 = array(1,3);
$result = array_diff($array1, $array2);
print_r($result);
?>
Demo
In Your style it can even be a one liner :P
<?php print_r(array_diff(array(1,2,3,4,6), array(1,3))); ?>

Problem with creating array using arrays

I have 2 arrays , $array1 & $array2. I want to create new array such that its key value is values of array1 and values are values of array2. Is it possible..
I used following approach to create this new array named $inputs. Is this correct?
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_fill("$array1",count($array1),$array2);
print_r($inputs);
If I read your question correctly, you can use array_combine() PHP Manual
$array1 = array("3","4","6");
$array2 = array("a","b","c");
$inputs = array_combine($array2, $array1); # keys, values
Use array_combine.
http://www.php.net/manual/de/function.array-combine.php
$inputs = array_combine($array1, $array2);
<?php
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_combine($array1,$array2);
print_r($inputs);
?>
http://codepad.org/6fTXCZa5
use php array_combine function
http://php.net/manual/en/function.array-combine.php

How to compare 2 different length arrays to eachother

I'm trying to make a function that compares two different length arrays to each other and if they match then some actions are performed. Array1, cell1 compares to array2, cell1, cell2, cellN... Then Array1, cell2 compares to array2, cell1, cell2, cellN...
Something resembling this:
if(array1[$i]==array2[])
{
// Some actions...
}
How can this be implemented?
PHP has in_array for searching an array for a particular value. So what about
foreach ($array1 as $search_item)
{
if (in_array($search_item, $array2))
{
// Some actions...
break;
}
}
You can get the difference of the Arrays with the PHP function array_diff.
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Results in
Array
(
[1] => blue
)
You can use nested loops for this.
for($i=0; $i<count($array1); $i++){
for($j=0; $j<count($array2); $j++){
if($array1[$i] == $array2[$j]){
//some action here
}
}
}
kindly correct the errors. im comparing the arrays values to display respectively with their paired one
if((cardnumb1[1]==123456789) && (passcode[1]==1234))
else if ((cardnumb1[2]==987654321) && (passcode[2]==4567))
else if ((cardnumb1[3]==123789456) && (passcode[3]==7890))
Even if answered I think, just for reference, is good to know that you can make:
$array_1 = array(1,2,3,4,5);
$array_2 = array(2,4,6,8);
foreach (array_intersect($array_1,$array_2) as $match){
//do something
}
NOTE: may give some problems with associative arrays.

Categories