Arrays - indexed and associative arrays, looping arrays, array operations - One Line Questions
1.
What is the difference between `isset($array['key'])` and `array_key_exists('key', $array)`? —
`isset` checks if the key exists AND its value is not NULL, `array_key_exists` only checks for existence.
2.
What is the primary difference between `sort()` and `asort()`? —
`sort` re-indexes numerically, `asort` maintains original keys.
3.
What is the primary difference between `unset($array[key])` and `array_pop($array)`? —
`unset` removes a specific element, `array_pop` specifically removes the last element.
4.
Consider `$arr = ['apple', 'banana', 'cherry'];`. What is the result of `implode(', ', $arr)`? —
'apple, banana, cherry'
5.
Consider `$array1 = [1, 2]; $array2 = [3, 4];`. What is the result of `array_merge($array1, $array2)`? —
[1, 2, 3, 4]
6.
Which syntax is used to declare an associative array in PHP? —
$student = ['id' => 101, 'name' => 'Bob'];
7.
Which of the following is an example of an indexed array declaration in PHP? —
$colors = array('red', 'green', 'blue');
8.
If you have an associative array like $config = ['host' => 'localhost', 'user' => 'root']; how do you access the username? —
$config['user']
9.
How would you access the value 'banana' in the following multi-dimensional array: `$fruits = [['apple', 'banana'], ['cherry', 'date']];`? —
$fruits[0][1]
10.
What is the default value for the `$length` parameter in `array_slice()` if omitted? —
The remaining elements from the offset
11.
What is the default starting index for an indexed array in PHP? —
0
12.
Consider `$data = ['a' => 1, 'b' => 2];`. What does `array_key_exists('a', $data)` return? —
TRUE
13.
What does the `range()` function create in PHP? —
An array containing a range of elements.
14.
Which PHP construct is commonly used to loop through all elements of an array, regardless of whether it's indexed or associative? —
A `foreach` loop.
15.
What is a multi-dimensional array in PHP? —
An array where each element is itself an array.
16.
Which function merges one or more arrays together? —
array_merge()
17.
Which function searches an array for a given value and returns the corresponding key if successful? —
array_search()
18.
Which function returns an array containing all the values of an array? —
array_values()
19.
Which function creates an array using one array for keys and another for values? —
array_combine()
20.
Which function is used to remove an element from an array by its index or key? —
unset()
21.
Which function is used to remove the last element from an array and return its value? —
array_pop()
22.
Which function can be used to add an element to the end of an indexed array? —
array_push()
23.
Which function returns an array containing all the keys of an array? —
array_keys()
24.
Which function sorts an associative array in reverse order according to its values? —
arsort()
25.
Which function sorts an array by its keys in ascending order? —
ksort()
26.
What distinguishes an associative array from an indexed array in PHP? —
Associative arrays use named keys (strings) instead of numeric indices.
27.
Which function is used to split a string into an array by a string separator? —
explode()
28.
Which function checks if a key or index exists in an array? —
array_key_exists()
29.
What does the following `foreach` loop do: `foreach ($array as $value) { echo $value; }`? —
It iterates through the array and prints only the values.
30.
What is the return value of `array_search()` if the value is not found in the array? —
FALSE
31.
What does `array_shift()` do? —
Removes the first element of an array and returns it.
32.
What does `in_array($needle, $haystack)` do? —
Checks if a value exists in an array.
33.
Which function sorts an array by its values in ascending order, maintaining key association? —
asort()
34.
Which PHP function is used to recursively sort an array by values, maintaining key association? —
asort()
35.
If you need to sort an array based on a custom comparison of values using a user-defined function, which sorting function should you use? —
usort()
36.
What does `rsort()` do to an array? —
Sorts values in descending order, re-indexing numerically.
37.
What does `implode('_', $array)` do? —
Joins array elements into a string using '_' as a separator.
38.
What is the result of printing an array directly using `print_r($myArray);`? —
A human-readable representation of the array's structure and content.
39.
When using `array_slice()` with a negative `$offset`, what does it indicate? —
The slice starts from the end of the array.
40.
What happens if `array_merge()` is used with associative arrays having the same keys? —
The values from the later array overwrite the values from the earlier array.
41.
For `array_combine()`, what is a crucial requirement for the input arrays? —
They must have the same number of elements.
42.
What is the primary use case for `array_splice()`? —
To remove and/or replace existing elements with new ones in an array.
43.
What is the purpose of `count($array)` or `sizeof($array)`? —
To get the number of elements in the array.
44.
What is the purpose of `array_slice($array, $offset, $length)`? —
To extract a portion of an array into a new array.
45.
What is the purpose of `array_unshift()`? —
To add one or more elements to the beginning of an array.
46.
What is the primary purpose of arrays in PHP? —
To store multiple values, accessible by an index or a key.
47.
How can you access both the key and the value during a `foreach` loop in PHP? —
Using `foreach ($array as $key => $value) { echo $key . ': ' . $value; }`.
48.
How can you access an element in an indexed array in PHP? —
Using its numeric index within square brackets, e.g., $array[0].
49.
In PHP, how are indexed arrays typically created? —
By assigning values to specific numeric indices.