PHP Arrays Flashcards
Q: What is an array in PHP?
A: A data structure that stores multiple values in a single variable.
Q: What are the three types of arrays in PHP?
A: Indexed arrays, associative arrays, and multidimensional arrays.
Q: How do you create an indexed array in PHP?
A: $array = array(“value1”, “value2”);.
Q: How do you create an associative array in PHP?
A: $array = array(“key1” => “value1”, “key2” => “value2”);.
Q: How do you access an element in an indexed array?
A: Use the index, e.g., $array[0];.
Q: What function is used to get the length of an array?
A: count().
Q: How do you check if a value exists in an array?
A: in_array($value, $array);.
Q: How do you check if a key exists in an array?
A: array_key_exists($key, $array);.
Q: What function is used to merge two arrays?
A: array_merge().
Q: How do you add an element to the end of an array?
A: array_push($array, $value);.
Q: What loop is commonly used to iterate over an array?
A: foreach.
Q: How do you loop through an array with foreach?
foreach ($array as $value) {
// Code
}
Q: How do you loop through an associative array with foreach?
foreach ($array as $key => $value) {
// Code
}
Q: What function applies a callback to each array element?
A: array_map().
Q: What function filters an array using a callback?
A: array_filter().
Q: What function retrieves all the keys of an array?
A: array_keys().