array লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান
array লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান

Difference between two arrays

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();

The array() function is used to create an array in PHP.

array syntax:

$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].".";

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>

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

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>

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

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>

The result of the above program -

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);
?>

The result of the above program -

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 );

Creation an Array 2:

$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);
}

Result:

66


Read More

Array Functions PHP

Array Functions PHP
PHP Array Functions

What is array?

An array is a special variable that can store multiple values in a single variable. It is very easy to create and access. A set of information you can store using an array. Using an array you don't need to declare many variable.

by declaring array() function you can easily create an array.

$array = array();

 Way to Store Value in an array:

$array = array('id' => 123, 'subject1' => 'Bangla', 'subject2' => 'English')

Why use Array Functions PHP?

In PHP, you can work with and manipulate arrays in different ways by using Array Functions. PHP arrays are important to the storage , management and execution of variables on sets. PHP supports simple and multidimensional arrays and can either be generated by the user or generated by another function.

How to Installation:

Array functions are part of the PHP core. That's why no need to installation to use these functions.

There are many PHP Array Functions in following:
  • array() Function - Use to create an array.
The php array() function is used to create an array. 
In PHP, there are three types of arrays: 
  1. Indexed arrays - Arrays with numeric index 
  2. Associative arrays - Arrays with named keys 
  3. Multidimensional arrays - Arrays containing one / more arrays

List of array Functions and descriptions:

  1. array() - Create an array.
  2. array_change_key_case() - Returns an array with all keys (lowercase or uppercase).
  3. array_chunk() - Splits / Divide an array into pieces of arrays.
  4. array_column() - In the input array return the values from a single column.
  5. array_combine() - It creates an array using one array for storing keys and another for storing its values.
  6. array_count_values()
  7. array_diff() - Returns the differences after Compares array values.
  8. array_diff_assoc() - Returns the differences after Compares array keys and values.
  9. array_diff_key() - Returns the differences after Compares array keys.
  10. array_diff_uassoc()
  11. array_diff_ukey()
  12. array_fill() - It fills an array with values.
  13. array_fill_keys()
  14. array_filter()
  15. array_flip()
  16. array_intersect() - Returns the matches after Compares array values.
  17. array_intersect_assoc() - Returns the matches after Compares array keys and values.
  18. array_intersect_key() - Returns the matches after Compares array keys.
  19. array_intersect_uassoc()
  20. array_intersect_ukey()
  21. array_key_exists() - Checks the specific key exists in the array.
  22. array_keys() - It returns all the keys of an array.
  23. array_map()
  24. array_merge() - It creates an array by merging one or more array.
  25. array_merge_recursive()
  26. array_multisort() -  Used to Sort multiple or multi-dimensional arrays.
  27. array_pad()
  28. array_pop()
  29. array_product()
  30. array_push()
  31. array_rand()
  32. array_reduce()
  33. array_reverse()
  34. array_search()
  35. array_shift()
  36. array_slice()
  37. array_splice()
  38. array_sum()
  39. array_udiff()
  40. array_udiff_assoc()
  41. array_udiff_uassoc()
  42. array_uintersect()
  43. array_uintersect_assoc()
  44. array_uintersect_uassoc()
  45. array_unique()
  46. array_unshift()
  47. array_values()
  48. array_walk()
  49. array_walk_recursive()
  50. arsort()
  51. asort()
  52. compact()
  53. count()
  54. current()
  55. each()
  56. end()
  57. extract()
  58. in_array()
  59. key()
  60. krsort()
  61. ksort()
  62. list()
  63. natcasesort()
  64. natsort()
  65. next()
  66. pos()
  67. prev()
  68. range()
  69. reset()
  70. rsort()
  71. shuffle()
  72. sizeof()
  73. sort()
  74. uasort()
  75. uksort()
  76. usort()
In the following I have explained some PHP array functions.
  • array_change_key_case() Function - This array function Changes all keys in an array to lowercase / uppercase.
Syntax:
array_change_key_case(array, CASE_LOWER/CASE_UPPER)
// CASE_LOWER - Default value. Changes the keys to lowercase.
//CASE_UPPER - Changes the keys to uppercase.
Change all keys in an array to uppercase:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$array=array("Bangla"=>"05","Tutorials"=>"12","Point"=>"23"); print_r(array_change_key_case($array,CASE_UPPER)); 
?> 
</body> 
</html>

Result:
Array ( [BANGLA] => 05 [TUTORIALS] => 12 [POINT] => 23 )
  • array_chunk() Function - This array function Splits an array into chunks of arrays.
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$university=array("IUBAT","BUET","BUBT","DUET","DU","RUET"); 
echo "<pre>"; 
print_r(array_chunk($university,2)); 
?> 
</body> 
</html>

Result:
Array ( 
[0] => Array 
[0] => IUBAT 
[1] => BUET 
[1] => Array 
[0] => BUBT 
[1] => DUET 
[2] => Array 
[0] => DU 
[1] => RUET 
)
  • array_column() Function - This array function Returns the values from a single column in the input array.
Syntax:
array_column(array, column_key, index_key)
// array - required, column_key - required, index_key - Optional.
Get column of last names from a recordset:
<?php
$a = array( 
array( 
'id' => 123, 
'f_name' => 'Bangla', 
'l_name' => 'English', 
), 
array( 
'id' => 456, 
'f_name' => 'Mathematics', 
'l_name' => 'Physics', 
), 
array( 
'id' => 789, 
'f_name' => 'Biology', 
'l_name' => 'Psychology', 
); 
$l_name = array_column($a, 'l_name'); 
print_r($l_name); 
?>
Result:
Array ( 
[0] => English
[1] => Physics
[2] => Psychology
)
Indexed by the "id" column, Get the column of last names from a recordset,
<?php
$a = array( 
array( 
'id' => 123, 
'f_name' => 'Bangla', 
'l_name' => 'English', 
), 
array( 
'id' => 456, 
'f_name' => 'Mathematics', 
'l_name' => 'Physics', 
), 
array( 
'id' => 789, 
'f_name' => 'Biology', 
'l_name' => 'Psychology', 
); 
$l_name = array_column($a, 'l_name', 'id'); 
print_r($l_name); 
?>
Result:
Array ( 
[123] => English
[456] => Physics
[789] => Psychology
)
  • array_combine() function - This array function Creates an array by using the elements from one "keys" array and one "values" array.
By using the elements from one "keys" array and one "values" array, Create an array :
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$first_name=array("Bangla","Tutorials","Point"); 
$age=array("40","52","42"); 
$result=array_combine($first_name,$age); 
print_r($result); 
?> 
</body> 
</html>
Result:
Array ( 
[Bangla] => 40 
[Tutorials] => 52 
[Point] => 42 
)
  • array_count_values() Function - This array function Count all the values of an array:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$university=array("IUBAT","DU","IUBAT","DU","RUET"); print_r(array_count_values($university)); 
?> 
</body> 
</html>
Result:
Array ( 
[IUBAT] => 2 
[DU] => 2 
[RUET] => 1 
)
    • array_diff() Function - This array function Compare the values of two arrays, and return the differences:
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $university1=array("IUBAT","BUET","DU"); 
    $university2=array("IUBAT","DU"); $result=array_diff($university1,$university2); 
    print_r($result); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [1] => BUET 
    )
    • array_diff_assoc() Function - This array Function Compare arrays, and returns the differences (compare keys and values)
    Compare the keys and values of two arrays, and return the differences:
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $university1=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","d"=>"DUET"); $university2=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","e"=>"DUET"); $result=array_diff_assoc($university1,$university2); 
    print_r($result); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [d] => DUET 
    )
    • array_diff_key() Function - This array function Compare arrays, and returns the differences (compare keys only)
    Return the differences after Compare the keys of two arrays:
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $university1=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","d"=>"DUET"); $university2=array("a"=>"IUBAT","b"=>"BUET","e"=>"DUET"); $result=array_diff_key($university1,$university2); 
    print_r($result); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [c] => DU 
    [d] => DUET 
    )
    • array_diff_uassoc() Function - This array function Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)
    • array_diff_ukey() Function - This function Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)
    • array_fill() Function - This array function Fills an array with values:
    Return the differences after Compare the keys of two arrays:
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $array1=array_fill(0,5,"IUBAT"); 
    $array2=array_fill(0,1,"DUET"); 
    print_r($array1); 
    echo "<br>";
    print_r($array2); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [0] => IUBAT 
    [1] => IUBAT 
    [2] => IUBAT 
    [3] => IUBAT 
    [4] => IUBAT 
    )

    Array ( 
    [0] => DUET 
    )
    • array_fill_keys() Function - This array function Fill an array with values, specifying keys:
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $keys=array("x","y","z","w"); 
    $result=array_fill_keys($keys,"IUBAT"); 
    print_r($result); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [x] => IUBAT 
    [y] => IUBAT 
    [z] => IUBAT 
    [w] => IUBAT
     )
    • array_filter() Function - This array function Filter the values of an array using a callback function.
    • array_flip() Function -  This array function Flips/Exchanges all keys with their associated values in an array.
    <!DOCTYPE html> 
    <html> 
    <body> 
    <?php 
    $university=array("x"=>"IUBAT","y"=>"BUET","z"=>"DUET","w"=>"RUET"); $result=array_flip($university); 
    print_r($result); 
    ?> 
    </body> 
    </html>
    Result:
    Array ( 
    [IUBAT] => x 
    [BUET] => y 
    [DUET] => z 
    [RUET] => w 
    )
    Read More