Arrays Flashcards

0
Q

What will happen when you try running the following?
$var = (object) “house” ;
$ varArray = array ( $var => “ car” );

A

The code will throw an illegal offset type , objects and arrays will throw this error because they cannot be used as keys in arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q
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
A

B and C .

Objects and Arrays can not be used as Keys in an array

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

Which of the following Key types must you not quote when referencing an array ?

A

Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.

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

What happens when you try to convert an object into an array?

A

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.

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

What happens when you convert integer, float , string , reaource to an array?

A

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.

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

What is array_diff() used for?

A

array_diff( array1 , array2 , …)

Computes the difference of arrays

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

What happens when following code is run?
“green”, “red”, “blue”);
$array2 = array(“b” => “green”, “yellow”, “red”);
$result = array_intersect($array1, $array2);
print_r($result);
?>

A

The following will be output ..

Array
(
    [a] => green
    [0] => red
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will be the result of the following code?
$array = array(“blue”, “red”, “green”, “blue”, “blue”);
print_r(array_keys($array, “blue”));

A
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the variable $result after the following function is applied?

$input = array(12, 10, 9);

$result = array_pad($input, 5, 0);

A

$result is array(12, 10, 9, 0, 0)

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

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);
A

$key1 = 2;

$key2 = 1;

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

What is the function in_array() used for ?

A

in_array () — Used to check if a value exists in an array

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

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

a => apple
b => banana
c => cranberry

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