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 • ARRAYSSubdividing an ArrayThe array_chunk() function breaks input_array into a multidimensional array that includes severalsmaller arrays consisting of size elements. Its prototype follows:array array_chunk(array array, int size [, boolean preserve_keys])If the input_array can’t be evenly divided by size, the last array will consist of fewer than sizeelements. Enabling the optional parameter preserve_keys will preserve each value’s corresponding key.Omitting or disabling this parameter results in numerical indexing starting from zero for each array. Anexample follows:$cards = array("jh", "js", "jd", "jc", "qh", "qs", "qd", "qc","kh", "ks", "kd", "kc", "ah", "as", "ad", "ac");// shuffle the cardsshuffle($cards);// Use array_chunk() to divide the cards into four equal "hands"$hands = array_chunk($cards, 4);print_r($hands);This returns the following (your results will vary because of the shuffle):Array ( [0] => Array ( [0] => jc [1] => ks [2] => js [3] => qd )[1] => Array ( [0] => kh [1] => qh [2] => jd [3] => kd )[2] => Array ( [0] => jh [1] => kc [2] => ac [3] => as )[3] => Array ( [0] => ad [1] => ah [2] => qc [3] => qs ) )SummaryArrays play an indispensable role in programming and are ubiquitous in every imaginable type ofapplication, web-based or not. The purpose of this chapter was to bring you up to speed regarding manyof the <strong>PHP</strong> functions that will make your programming life much easier as you deal with these arrays.The next chapter focuses on yet another very important topic: object-oriented programming. Thistopic has a particularly special role in <strong>PHP</strong> 5 because the process was entirely redesigned for this majorrelease.133

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

Saved successfully!

Ooh no, something went wrong!