Page 172 - Open Soource Technologies 304.indd
P. 172

Web Technologies-I



                   Notes         Entering second (stack is now: third -> second)
                                 Entering first (stack is now: third -> second -> first)

                                 Exiting
                                 Exiting
                                 Entering first (stack is now: third -> first)

                                 Exiting
                                 Exiting
                                 7.7.2 Sets


                                 Arrays let you implement the basic operations of set theory: union, intersection, and difference.
                                 Each set is represented by an array, and various PHP functions implement the set operations.
                                 The values in the set are the values in the array the keys are not used, but they are generally
                                 preserved by the operations.

                                 The union of two sets is all the elements from both sets, with duplicates removed. The array_
                                 merge( ) and array_unique( ) functions let you calculate the union. Here’s how to find the union
                                 of two arrays:

                                 function array_union($a, $b)

                                 {

                                 $union = array_merge($a, $b); // duplicates may still exist
                                 $union = array_unique($union); return $union;

                                 }

                                 $first = array(1, ‘two’, 3);

                                 $second = array(‘two’, ‘three’, ‘four’);
                                 $union = array_union($first, $second);

                                 print_r($union);

                                 Array ( [0] => 1 [1] => two [2] => 3[4] => three [5] => four )

                                 The intersection of two sets is the set of elements they have in common. PHP’s built-in array_
                                 intersect( ) function takes any number of arrays as arguments and returns an array of those values
                                 that exist in each. If multiple keys have the same value, the first key with that value is preserved.

                                 Another common function to perform on a set of arrays is to get the difference; that is, the values
                                 in one array that are not present in another array. The array_diff( ) function calculates this,
                                 returning an array with values from the first array that are not present in the second.

                                 The following code takes the difference of two arrays:
                                 $first = array(1, ‘two’, 3);

                                 $second = array(‘two’, ‘three’, ‘four’);




        166                               LOVELY PROFESSIONAL UNIVERSITY
   167   168   169   170   171   172   173   174   175   176   177