5. Arrays Flashcards

1
Q

How do you append a value to an array?

A

$arr[] = “Appended”;

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

What 2 functions can you use to get the size of an array?

A

count()

sizeof()

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

How can you pad an array with values?

A

array_pad($arr, min_elements, pad_value)

The min_elements can be a negative value as well, it will pad to the LEFT instead of RIGHT.

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

How can you copy array values to a list of variables?

A

list($first, $second) = $array

You can skip elements by just leaving that spot empty

Ex. list($first, , $third) = $array

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

How can you slice an array?

A

array_slice($arr, startIndex, numElements)

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

How can you split an array into multiple smaller arrays?

A

array_chunk($arr, arr_size)

This will return an array of arrays of the size you specified. There’s also an optional parameter to preserve keys which could be useful for associative arrays.

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

How can we list the keys of an array?

A

array_keys($arr)

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

How can we retrieve JUST the values from an array all in one function?

A

array_values($array)

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

How can you cut values out of an array and store in new array?

A

array_splice($array, start, length)

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

How do you create variables from an associative array?

A

extract($array)

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

How do you create an array from values?

Think inverse of creating variables from an array.

A

compact(“var1”, “var2”, ..etc)

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

How can you write a foreach loop for each element in an array?

A

foreach($array as $key => $value)

You can omit $key if you don’t need it.

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

How would you use a for loop on an array?

A

$arrayCount = count($array);

for ($i = 0; $i < $arrayCount; $i++)

Best to move the count outside of the for statement.

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

How can you call a function for each element in an array?

A

array_walk($array, “functionToCall”, “extraDataPassToFunction”)

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

How do you apply a function to each element in an array to turn it into a single value?

A

array_reduce($array, “functionToCall”, $initialValue)

functionToCall should take $runningTotal & $currentValue

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

How can you determine if a value exists in an array?

A

in_array(“Target To Find”, $arrayToSearch)

17
Q

How can you allow a class you make to be used in a foreach loop?

A

implement Iterator