array functions Flashcards

1
Q

is_array

A

Arrays and variables share the same namespace. This means that you cannot have a string variable called $fred and an array also called $fred, If you’re doubt and your code needs to check whether a variable is an array, you can use the is_array function like this:

echo (is_array($fred)) ? “Is an array” : “Is not an array”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

count

A

To count all the elements in the top level of an array, use a command such as the following:

echo count($fred);

Should you wish to know how many elements there are altogether in a multidimensional array, you can use a statement such as:

echo count($fred, 1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

sort

A

To sort an array:

sort($fred);
sort($fred, SORT_NUMERIC);
sort($fred, SORT_STRING);

To sort an array in reverse order:

rsort($fred, SORT_NUMERIC);
rsort($fred, SORT_STRING);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

shuffle

A

To put an array in random order:

shuffle($cards);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

explode

A

Takes a string containing several items separated by a single character (or a string of characters) and then places each of these items into an array.

$temp = explode(‘ ‘, “This is a sentence with seven words”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

extract

A

Assigns values of an array to new variables

extract($_GET);

To avoid the possibility of overwriting variables that already exist, you can prefix the new variables:

extract($_GET, EXTR_PREFIX_ALL, ‘fromget’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

compact

A

The inverse of extract, creates an array from variables and their values:

$contact = compact(‘fname’, ‘sname’, ‘planet’, ‘system’, ‘constellation’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

print_r(compact(explode()));

A

Printout of a group of variables’s values. Place the names of the variables inside explode separated by a character you choose.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

reset

A

When the foreach… as construct or the each function talks through an array, it keeps an internal PHP pointer that makes a note of which element of the array it should return next. If your code ever needs to return to the start of an array, you can issue reset, which returns the value of that element.

reset($fred); // throw away return value
$item = reset($fred); // keep first element of the array in $item

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

end

A

Move PHP’s internal array pointer to the final element in an array, which also returns the value of the element:

end($fred);
$item = end($fred);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly