You are on page 1of 9

What are Arrays?

 An array can hold multiple, separate pieces of information

 List of values

 Each value being a string or a number or even another array

 Structured as a series of key-value pairs.

 Each item in the list, there is a key associate with it.

 Support 2 kind of arrays

 Indexed – use numbers as the key

 Associative – use strings as key

Array and strings

 2 functions for converting between strings and arrays

 $array = explode (separator, $string);

• Turning a string into an array

 $string =‘Jan-Feb-Mac-Apr-May’;

 $array =explode (‘-’, $string);

 $string = implode (glue, $array);

• Turning an array to string

 $string =implode (‘ , ’, $array);

 The $string consists of Jan,Feb,Mac,Apr,May;


 Example – indexed

 $artists
 Example – associative

 $states
Sorting Arrays

 Use built in PHP function

 sort() – sort an array by value discarding the original keys

• The array keys will be reset after the sorting process

 Avoid use this function if key-value relationship is important.

 asort() – sort an array by value while maintaining the

key

 ksort() – sort an array by key

 Each of these can sort reverse order by changing them to

• rsort() , arsort() , krsort()

 shuffle() – to randomize the order of an array

You might also like