PHP Arrays Flashcards

1
Q

Q: What is an array in PHP?

A

A: A data structure that stores multiple values in a single variable.

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

Q: What are the three types of arrays in PHP?

A

A: Indexed arrays, associative arrays, and multidimensional arrays.

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

Q: How do you create an indexed array in PHP?

A

A: $array = array(“value1”, “value2”);.

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

Q: How do you create an associative array in PHP?

A

A: $array = array(“key1” => “value1”, “key2” => “value2”);.

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

Q: How do you access an element in an indexed array?

A

A: Use the index, e.g., $array[0];.

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

Q: What function is used to get the length of an array?

A

A: count().

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

Q: How do you check if a value exists in an array?

A

A: in_array($value, $array);.

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

Q: How do you check if a key exists in an array?

A

A: array_key_exists($key, $array);.

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

Q: What function is used to merge two arrays?

A

A: array_merge().

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

Q: How do you add an element to the end of an array?

A

A: array_push($array, $value);.

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

Q: What loop is commonly used to iterate over an array?

A

A: foreach.

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

Q: How do you loop through an array with foreach?

A

foreach ($array as $value) {
// Code
}

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

Q: How do you loop through an associative array with foreach?

A

foreach ($array as $key => $value) {
// Code
}

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

Q: What function applies a callback to each array element?

A

A: array_map().

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

Q: What function filters an array using a callback?

A

A: array_filter().

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

Q: What function retrieves all the keys of an array?

A

A: array_keys().

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

Q: What function retrieves all the values of an array?

A

A: array_values().

18
Q

Q: How do you combine two arrays into key-value pairs?

A

A: array_combine($keys, $values);.

19
Q

Q: How do you flip the keys and values of an array?

A

A: array_flip().

20
Q

Q: What function checks if a specific key exists in an array?

A

A: array_key_exists().

21
Q

Q: How do you sort an array by values in ascending order?

A

A: sort($array);.

22
Q

Q: How do you sort an array by values in descending order?

A

A: rsort($array);.

23
Q

Q: How do you sort an array by keys in ascending order?

A

A: ksort($array);.

24
Q

Q: How do you sort an associative array by values in ascending order?

A

A: asort($array);.

25
Q

Q: What function sorts an array using a custom comparison function?

A

A: usort().

26
Q

Q: How do you remove the last element of an array?

A

A: array_pop($array);.

27
Q

Q: How do you remove the first element of an array?

A

A: array_shift($array);.

28
Q

Q: How do you add an element to the beginning of an array?

A

A: array_unshift($array, $value);.

29
Q

Q: How do you slice a portion of an array?

A

A: array_slice($array, $offset, $length);.

30
Q

Q: How do you replace part of an array with another array?

A

A: array_splice($array, $offset, $length, $replacement);.

31
Q

Q: What function searches for a value and returns its key?

A

A: array_search($value, $array);.

32
Q

Q: What function checks if a value exists in an array?

A

A: in_array($value, $array);.

33
Q

Q: How do you find the index of the first occurrence of a value in an array?

A

A: Use array_search().

34
Q

Q: How do you get the first key of an array?

A

A: array_key_first($array);.

35
Q

Q: How do you get the last key of an array?

A

A: array_key_last($array);.

36
Q

Q: What is a multidimensional array?

A

A: An array containing one or more arrays as elements.

37
Q

Q: How do you access a value in a multidimensional array?

A

A: Use multiple indexes, e.g., $array[0][‘key’];.

38
Q

Q: How do you loop through a multidimensional array?

A

A: Use nested foreach loops.

39
Q

Q: What function recursively merges multidimensional arrays?

A

A: array_merge_recursive().

40
Q

Q: What function applies a callback recursively to an array?

A

A: array_walk_recursive().