11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 5 • ARRAYS$subset = array_splice($states, 2, -1, array("New York", "Florida"));print_r($states);This returns the following:Array ( [0] => Alabama [1] => Alaska [2] => New York[3] => Florida [4] => Connecticut )Calculating an Array IntersectionThe array_intersect() function returns a key-preserved array consisting only of those values present inthe first array that are also present in each of the other input arrays. Its prototype follows:array array_intersect(array array1, array array2 [, arrayN])The following example will return all states found in the $array1 that also appear in $array2 and$array3:$array1 = array("OH", "CA", "NY", "HI", "CT");$array2 = array("OH", "CA", "HI", "NY", "IA");$array3 = array("TX", "MD", "NE", "OH", "HI");$intersection = array_intersect($array1, $array2, $array3);print_r($intersection);This returns the following:Array ( [0] => OH [3] => HI )Note that array_intersect() considers two items to be equal only if they also share the same datatype.TIP. Introduced in <strong>PHP</strong> 5.1.0, the array_intersect_key() function will return keys located in an arraythat are located in any of the other provided arrays. The function’s prototype is identical toarray_intersect(). Likewise, the array_intersect_ukey() function allows you to compare the keys ofmultiple arrays with the comparison algorithm determined by a user-defined function. Consult the <strong>PHP</strong>manual for more information.Calculating Associative Array IntersectionsThe function array_intersect_assoc() operates identically to array_intersect(), except that it alsoconsiders array keys in the comparison. Therefore, only key/value pairs located in the first array that arealso found in all other input arrays will be returned in the resulting array. Its prototype follows:array array_intersect_assoc(array array1, array array2 [, arrayN])129

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!