I have following two arrays. I want the difference between these two arrays. That is, how can I find the values that do not exist in one array?
In this post, i'm going to show about how to difference between tow arrays. At first you have to create two array. In the following example, i have created two array named "$arr1" and "$arr2". Then i have fetched the values from database and stored the values in two arrays. Finally, i have used "array_diff()" function to differentiate the arrays.
For this purpose, at first i am going to describe what an array is. array type and description of different type of array with examples.
After describing about array i will show the difference between two arrays.
What is an array?
An array is a variable but it is special variable, because that can hold more than one value at a time. Using an array you can store many value in a single variable. For instance if you want to store 50 numbers then then you have to create 50 variables. But using an array there is no need to create 50 variables because an array can hold more different different value.
Creating an array:
array();
array();
The array() function is used to create an array in PHP.
array syntax:
$numbers = array("1", "2", "3");
$numbers = array("1", "2", "3");
There is an array named "$numbers". I have assigned three values in this variable. Now if you need to echo the each value of this variable then you can use the following syntex.
Print the array values:
echo "The values are".$numbers[0]. ", ".$numbers[1].", ".$numbers[2].".";
echo "The values are".$numbers[0]. ", ".$numbers[1].", ".$numbers[2].".";
Actually the array values can be access with the index number. Index numbers always start with 0.
Types of array:
There are three different types of arrays and each array value is accessed using an array index number that always starts with 0.
Indexed arrays - Indexed arrays can be stored with numbers, strings and any object but their index will be represented by numbers. Index numbers always start with 0.
Here array() function to create array. I have used two method to create an array in the following.
<html>
<body>
<?php
/* First method to create an array named array1. */
$array1 = array( 6, 7, 8, 9, 10);
foreach( $array1 as $value ) {
echo "Value of array1 is $value <br />";
}
/* Second method to create an array named array2. */
$array2[0] = "one";
$array2[1] = "two";
$array2[2] = "three";
$array2[3] = "four";
$array2[4] = "five";
foreach( $array2 as $value ) {
echo "Value of array2 is $value <br />";
}
?>
</body>
</html>
<html>
<body>
<?php
/* First method to create an array named array1. */
$array1 = array( 6, 7, 8, 9, 10);
foreach( $array1 as $value ) {
echo "Value of array1 is $value <br />";
}
/* Second method to create an array named array2. */
$array2[0] = "one";
$array2[1] = "two";
$array2[2] = "three";
$array2[3] = "four";
$array2[4] = "five";
foreach( $array2 as $value ) {
echo "Value of array2 is $value <br />";
}
?>
</body>
</html>
The result of the above program -
Value of array1 is 6
Value of array1 is 7
Value of array1 is 8
Value of array1 is 9
Value of array1 is 10
Value of array2 is one
Value of array2 is two
Value of array2 is three
Value of array2 is four
Value of array2 is five
Value of array1 is 6
Value of array1 is 7
Value of array1 is 8
Value of array1 is 9
Value of array1 is 10
Value of array2 is one
Value of array2 is two
Value of array2 is three
Value of array2 is four
Value of array2 is five
Associative arrays - In term of functionality, the associative arrays are very similar to Index arrays but Associative array will have key associated with values.
Suppose there are many employees in a institute. Now you want to store the salaries of the employees. To store the salaries of employees you need to create an array, for this purpose you need to create associative array because index array is not the best choice.
To store the salaries of employees you need to store employees name with salary amount.
<html>
<body>
<?php
/* First method to associate create array. */
$array1 = array("Azom" => 20000, "tutorial" => 10000, "stars" => 5000);
echo "Salary of Azom is ". $array1['Azom'] . "<br />";
echo "Salary of tutorial is ". $array1['tutorial']. "<br />";
echo "Salary of stars is ". $array1['stars']. "<br />";
/* Second method to create array. */
$array2['Azom'] = "high";
$array2['tutorial'] = "medium";
$array2['stars'] = "low";
echo "Salary of Azom is ". $array2['Azom'] . "<br />";
echo "Salary of tutorial is ". $array2['tutorial']. "<br />";
echo "Salary of stars is ". $array2['stars']. "<br />";
?>
</body>
</html>
<html>
<body>
<?php
/* First method to associate create array. */
$array1 = array("Azom" => 20000, "tutorial" => 10000, "stars" => 5000);
echo "Salary of Azom is ". $array1['Azom'] . "<br />";
echo "Salary of tutorial is ". $array1['tutorial']. "<br />";
echo "Salary of stars is ". $array1['stars']. "<br />";
/* Second method to create array. */
$array2['Azom'] = "high";
$array2['tutorial'] = "medium";
$array2['stars'] = "low";
echo "Salary of Azom is ". $array2['Azom'] . "<br />";
echo "Salary of tutorial is ". $array2['tutorial']. "<br />";
echo "Salary of stars is ". $array2['stars']. "<br />";
?>
</body>
</html>
The result of the above program -
Salary of Azom is 20000
Salary of tutorial is 10000
Salary of stars is 5000
Salary of Azom is high
Salary of tutorial is medium
Salary of stars is low
Salary of Azom is 20000
Salary of tutorial is 10000
Salary of stars is 5000
Salary of Azom is high
Salary of tutorial is medium
Salary of stars is low
Multidimensional arrays - Multidimensional Array contains one or more arrays.
Suppose there are three students in a school. Now, you need to keep the students marks in an array That's why you need to create multidimensional array to store the students marks. Assume each student have 3 subjects as like Bangla, English, physics, mathematics, chemistry and biology. Now you need to keep particular student marks into array you have to create multidimensional array.
<html>
<body>
<?php
$marks = array(
"Amit" => array (
"bangla" => 80,
"english" => 85,
"chemistry" => 90
),
"Sarker" => array (
"physics" => 87,
"maths" => 67,
"chemistry" => 98
),
"Antu" => array (
"physics" => 56,
"maths" => 62,
"chemistry" => 85
)
);
/* Accessing multi-dimensional array values */
echo "Marks for Amit in physics : " ;
echo $marks['Amit']['physics'] . "<br />";
echo "Marks for Sarker in maths : ";
echo $marks['Sarker']['maths'] . "<br />";
echo "Marks for Antu in chemistry : " ;
echo $marks['Antu']['chemistry'] . "<br />";
?>
</body>
</html>
<html>
<body>
<?php
$marks = array(
"Amit" => array (
"bangla" => 80,
"english" => 85,
"chemistry" => 90
),
"Sarker" => array (
"physics" => 87,
"maths" => 67,
"chemistry" => 98
),
"Antu" => array (
"physics" => 56,
"maths" => 62,
"chemistry" => 85
)
);
/* Accessing multi-dimensional array values */
echo "Marks for Amit in physics : " ;
echo $marks['Amit']['physics'] . "<br />";
echo "Marks for Sarker in maths : ";
echo $marks['Sarker']['maths'] . "<br />";
echo "Marks for Antu in chemistry : " ;
echo $marks['Antu']['chemistry'] . "<br />";
?>
</body>
</html>
The result of the above program -
Marks for Amit in bangla : 80
Marks for Sarker in maths : 67
Marks for Antu in chemistry : 85
Marks for Amit in bangla : 80
Marks for Sarker in maths : 67
Marks for Antu in chemistry : 85
Difference Between Two array:
What is array_diff()?
array_diff() function is used to Compare the values of two arrays and return the differences of two arrays.
<?php
$array1=array("a"=>"bangla","b"=>"english","c"=>"mathematics","d"=>"physics");
$array2=array("e"=>"bangla","f"=>"english","g"=>"mathematics");
$result=array_diff($a1,$a2);
print_r($result);
?>
<?php
$array1=array("a"=>"bangla","b"=>"english","c"=>"mathematics","d"=>"physics");
$array2=array("e"=>"bangla","f"=>"english","g"=>"mathematics");
$result=array_diff($a1,$a2);
print_r($result);
?>
The result of the above program -
difference result between array1 and array 2 is = physics
difference result between array1 and array 2 is = physics
array_diff() function compares the values of two or more arrays, and return an array that contains the entries from array that are not present in array1 or array2, etc.
Actually array_diff() function is used to differentiate array from other arrays and return the differences.
Suppose you are going to add student list in class eight but some students ware failed in class seven. Now if you want to show which student are passed and they are admitted in class eight. then you can easily use array_diff() function to differentiate the students.
In the following, I am going to real example to differentiate array values.
Creating an Array 1:
$array1 = Array ( [0] => 64[1] => 65 [2] => 66 );
$array1 = Array ( [0] => 64[1] => 65 [2] => 66 );
Creation an Array 2:
$array2 = Array ( [0] => 64[1] => 65 );
$array2 = Array ( [0] => 64[1] => 65 );
There is the Solution to differentiate between two arrays :
$checkcoupon = $con->query(" SELECT `coupon_number` FROM `add_users_coupon` WHERE `user_type_id`='1' AND `package_id`='1' ");
$arr1 = array();
while($row=$checkcoupon->fetch_assoc()){
$arr1[] = $row["coupon_number"];
}
$usedcoupon = $con->query(" SELECT `coupon_no` AS `coupon_number` FROM `used_coupons` WHERE `user_id`='$userID' AND `package_id`='$checkSubs' ");
$arr2 = array();
while($row=$usedcoupon->fetch_assoc()){
$arr2[] = $row["coupon_number"];
}
$final_result = array_diff($arr1, $arr2);
foreach ($final_result as $value) {
print_r($value);
}
$checkcoupon = $con->query(" SELECT `coupon_number` FROM `add_users_coupon` WHERE `user_type_id`='1' AND `package_id`='1' ");
$arr1 = array();
while($row=$checkcoupon->fetch_assoc()){
$arr1[] = $row["coupon_number"];
}
$usedcoupon = $con->query(" SELECT `coupon_no` AS `coupon_number` FROM `used_coupons` WHERE `user_id`='$userID' AND `package_id`='$checkSubs' ");
$arr2 = array();
while($row=$usedcoupon->fetch_assoc()){
$arr2[] = $row["coupon_number"];
}
$final_result = array_diff($arr1, $arr2);
foreach ($final_result as $value) {
print_r($value);
}
Result:
66
66
EmoticonEmoticon