[ Team LiB ] Previous Section Next Section

Workshop

Quiz

1:

Which construct can you use to define an array?

2:

What is the index number of the last element of the array defined here?


$users = array ("Harry", "Bob", "Sandy");

3:

Without using a function, what would be the easiest way of adding the element "Susan" to the $users array defined previously?

4:

Which function could you use to add the string "Susan" to the $users array?

5:

How would you find out the number of elements in an array?

6:

How would you loop through an array?

7:

Which function would you use to merge two arrays?

8:

How would you sort an associative array by its keys?


Answers

A1:

You can create an array with the array() construct.

A2:

The last element is $users[2]. Remember that arrays are indexed from zero by default.

A3:

You should use $users[] = "Susan";.

A4:

You should use array_push( $users, "Susan" );.

A5:

You can count the number of elements in an array with the count() function.

A6:

You can loop through an array using the foreach statement.

A7:

You can merge arrays with the array_merge() function.

A8:

You can sort an associative array by its keys with the ksort() function.


    [ Team LiB ] Previous Section Next Section