Arrays Flashcards
What will happen when you try running the following?
$var = (object) “house” ;
$ varArray = array ( $var => “ car” );
The code will throw an illegal offset type , objects and arrays will throw this error because they cannot be used as keys in arrays.
Which of the following cannot be used as a key in an array? Choose all that apply. A) integer B) Array C) object D) String
B and C .
Objects and Arrays can not be used as Keys in an array
Which of the following Key types must you not quote when referencing an array ?
Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.
What happens when you try to convert an object into an array?
If an object is converted to an array, the result is an array whose elements are the object’s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a ‘*’ prepended to the variable name.
What happens when you convert integer, float , string , reaource to an array?
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted.
What is array_diff() used for?
array_diff( array1 , array2 , …)
Computes the difference of arrays
What happens when following code is run?
“green”, “red”, “blue”);
$array2 = array(“b” => “green”, “yellow”, “red”);
$result = array_intersect($array1, $array2);
print_r($result);
?>
The following will be output ..
Array ( [a] => green [0] => red )
What will be the result of the following code?
$array = array(“blue”, “red”, “green”, “blue”, “blue”);
print_r(array_keys($array, “blue”));
Array ( [0] => 0 [1] => 3 [2] => 4 )
What is the variable $result after the following function is applied?
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
$result is array(12, 10, 9, 0, 0)
What is the resulting key values after the following code is run?
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
$key1= array_search('green', $array); $key2 = array_search('red', $array);
$key1 = 2;
$key2 = 1;
What is the function in_array() used for ?
in_array () — Used to check if a value exists in an array
What will be final output for the following code?
$fruit = array(‘a’ => ‘apple’, ‘b’ => ‘banana’, ‘c’ => ‘cranberry’);
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo “$key => $val\n”;
}
a => apple
b => banana
c => cranberry