0% found this document useful (0 votes)
7 views

Arrays

Iiii

Uploaded by

fertahyanis4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays

Iiii

Uploaded by

fertahyanis4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Arrays

© 2008 Haim Michael


Constants & Arrays
What will the following script output?

<?php
define(index, "5");
$vec[5] = "BOMBO";
$vec['conto'] = "DONGO";
$vec["index"] = "CONGO";
print "The value is: ";
print $vec[index]."\n";
?>

A. BOMBO
B. DONGO
C. CONGO
D. 0
E. 5

© 2008 Haim Michael


Strings & Arrays
What will the following script output?

<?php
$vec='abcdefghijklmnopqrstuvwxyz';
echo $vec[7].$vec[0].$vec[8].$vec[12];
?>

A. haim
B. ibjn
C. aaaa
D. hhhh
E. mmmm

© 2008 Haim Michael


Avoid The Index
What will the following script output?

<?php
$vec= array();
$vec[]=4;
$vec[]=12;
$vec[]=32;
$sum=$vec[0]+$vec[1]+$vec[2];
echo $sum;
?>

A. 48
B. 0
C. Null
D. 16
E. 44

© 2008 Haim Michael


Associative Arrays
Associative arrays use _____ keys.

A. string
B. int
C. float
D. objects
E. compound

© 2008 Haim Michael


Array of Arrays
Given the following script which of the options reference “david” in a correct way?

<?php
$matrix = array
(
"en"=>"English",
"he"=>"Hebrew",
"ru"=>"Russian",
"borochov"=>array("Gogoya",1600,2008),
"green"=>array("john"=>array("moshe","david","salam"))
);
?>

A. $matrix["green"]["john"][1];
B. $matrix["green"]["john"][2];
C. $matrix[4][0][1];
D. $matrix[4][1][1];
E. $matrix[4][0][2];
© 2008 Haim Michael
Arrays For Each Loop
Given the following script what will be the output?

<?php
$sum = 0;
$vec = array(3,3);
foreach($vec as $key => &$val)
{
$sum += $val;
$val = 4;
}
foreach($vec as $key => $val)
$sum += $val;
echo $sum;
?>

A. 14
B. 12
C. Compilation Error
D. Compilation Warning
© 2008 Haim Michael
The serialize Function

Given the following script what will be the output?

<?php
$sum = 0;
$vec = array(100,90,80);
$ser = serialize($vec);
$arr = unserialize($ser);
foreach($arr as $key => $value)
{
$sum += $value;
}
echo $sum;
?>

A. 270
B. 0
C. Compilation Error
D. Compilation Warning
E. Null
© 2008 Haim Michael
The Native Sort
Given the following script what will be the output?

<?php
$vec = array('a12','a1','a2','a24');
natsort($vec);
foreach($vec as $val)
{
echo "$val ";
}
?>

A. a1 a2 a12 a24
B. a1 a12 a2 a24
C. a24 a12 a2 a1
D. Compilation Warning
E. Null

© 2008 Haim Michael


No Key
Given the following scrip what will be the output?

<?php
$array = array ('a'=>12,'6'=>9,1,8);
$sum = $array[6]+$array[7];
echo $sum;
?>

A. 10
B. 18
C. 19
D. Compilation Warning
E. Null

© 2008 Haim Michael


The array_intersect Function

Given the following script what will be the output?

<?php
$sum = 0;
$varA = array(2,4,6,3,12);
$varB = array(4,6,9,3);
$varC = array(3,2,6);
$varD = array_intersect($varA,$varB,$varC);
foreach($varD as $k => $v)
{
$sum += $k;
}
echo $sum;
?>

A. 1
B. 2
C. 3
D. 4
E. 5 © 2008 Haim Michael
The array_sum Function

Given the following scrip what will be the output?

<?php
$varA = array(2,2,1,2,1,1,2,2);
$varB = array_count_values($varA);
$sum = array_sum($varB);
echo $sum;
?>

A. 3
B. 4
C. 5
D. 6
E. 8

© 2008 Haim Michael


Float Values Cannot Be Keys
Given the following scrip what will be the output?

<?php
$array = array (1.112 => 'a', 1.2 => 'b', 1.9 => 'c');
echo count ($array);
?>

A. 1
B. 2
C. 3
D. 0
E. Null

© 2008 Haim Michael


Tricky Sum
Given the following scrip what will be the output?

<?php
$vec = array (1, 2, 3, 5, 8, 9);
$sum = 0;
for ($i = 0; $i < 3; $i++)
{
$sum += $vec[$vec[$i]];
}
echo $sum;
?>

A. 10
B. 6
C. 11
D. 12
E. Null

© 2008 Haim Michael


The + Operator
Given the following scrip what will be the output?

<?php
$vecA = array(2,1,1,4,2);
$vecB = array(3,2,1,8,3,1);
$vecC = $vecA + $vecB;
$sum = array_sum($vecC);
echo $sum;
?>

A. 10
B. 6
C. 11
D. 12
E. Null

© 2008 Haim Michael


The === Operator
Given the following scrip what will be the output?

<?php
$vecA = array(2,1,1,4,2);
$vecB = array(1,2,1,4,2);
$vecC = array(1,2,1,4,2);
$sum = 0;
if($vecA==$vecB) $sum++;
if($vecA===$vecC) $sum++;
if($vecB===$vecC) $sum++;
echo $sum;
?>

A. 0
B. 1
C. 2
D. 3
E. Null
© 2008 Haim Michael
The count Function 1/2

Given the following scrip what will be the output?

<?php
$vecA = array(2,1,4,2);
$vecB = array(1,2,1);
$vecC = array(1,2);
$matrix = array();
$matrix[0] = $vecA;
$matrix[1] = $vecB;
$matrix[2] = $vecC;
$sum = 0;
foreach($matrix as $val)
{
$sum += count($val);
}
echo $sum;
?>

© 2008 Haim Michael


The count Function 2/2

A. 9
B. 5
C. 3
D. 2
E. Null

© 2008 Haim Michael


The isset Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages = array(
"en"=>"English",
"he"=>"Hebrew",
"ru"=>"Russian",
"ro"=>"Romanian");
$vecLetters = array(
"a"=>"AAA",
"b"=>"BBB",
"c"=>"CCC",
"d"=>"DDD");

© 2008 Haim Michael


The isset Function 2/2
if(isset($vecNumbers['2']) && isset($vecLanguages['ru']))
$sum++;
if(isset($vecLetters['a']) &&
isset($vecNumbers[$vecNumbers[3]]))
$sum++;
if(!isset($vecLanguages['de'])) $sum++;
echo $sum;
?>

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael


The array_key_exists Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages =
array("en"=>"English","he"=>"Hebrew","ru"=>"Russian","ro"=>"
Romanian");
$vecLetters =
array("a"=>"AAA","b"=>"BBB","c"=>"CCC","d"=>"DDD");
If( array_key_exists('2',$vecNumbers) &&
array_key_exists('ru',$vecLanguages)) $sum++;
If( array_key_exists('a',$vecLetters) &&
array_key_exists($vecNumbers[3],$vecNumbers)) $sum++;
if(!array_key_exists('de',$vecLanguages))
$sum++;
echo $sum;
?>
© 2008 Haim Michael
The array_key_exists Function 2/2

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael


The in_array Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages = array(
"en"=>"English", "he"=>"Hebrew",
"ru"=>"Russian","ro"=>"Romanian");
$vecLetters =
array("a"=>"AAA","b"=>"BBB","c"=>"CCC","d"=>"DDD");
if(in_array("English",$vecLanguages)) $sum++;
if(in_array("AAA",$vecLetters)) $sum++;
if(in_array("EEE",$vecLetters)) $sum++;
if(in_array('4',$vecNumbers)) $sum++;
echo $sum;
?>

© 2008 Haim Michael


The in_array Function 2/2

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael


The sort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
sort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael
The asort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
asort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael
The rsort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
rsort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael
The shuffle Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,8=>3,0=>4,5=>22);
shuffle($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael
The foreach Loop

Given the following code what will be the output?

<?php
$vec=array(1,2,3,4);
foreach($vec as $k=>$v)
{
$v=8;
}
$sum=array_sum($vec);
echo $sum;
?>

A. 6
B. 7
C. 8
D. 10
E. 32

© 2008 Haim Michael


The sort Function

What will be the output of the following code?

<?php
$vec=array(5,2,3,4);
$temp=sort($vec);
var_dump($temp);
?>

A. bool(true)
B. 1,2,3,4
C. Null
D. 1
E. Compilation Warning

© 2008 Haim Michael


Array Elements Assignment
What will be the output of the following code?

<?php
$vec = array();
$vec[] = "italy";
$vec[] = "russia";
$vec[] = "canada";
$str;
foreach($vec as $k=>$v)
$str = $str." ".$v;
echo $str;
?>

A. italy russia canada


B. russia
C. Null
D. empty string
E. canada
© 2008 Haim Michael
Simple Native Sort
What will be the output of the following code?

<?php
$vec = array("pix14.png", "pix10.png", "pix3.png",
"pix1.png");
natsort($vec);
foreach($vec as $k=>$v)
{
echo $v." ";
}
?>

A. pix1.png pix3.png pix10.png pix14.png


B. pix3.png pix1.png pix10.png pix14.png
C. Null
D. empty string
E. pix14.png pix3.png pix10.png pix1.png

© 2008 Haim Michael


The array_intersect Function

Given the following scrip what will be the output?

<?php
$sum = 0;
$varA = array('a','b',6,32,'asd');
$varB = array('b',6,9,3);
$varC = array(3,2,6,'asd');
$varD = array_intersect($varA,$varB,$varC);
foreach($varD as $k => $v)
{
$sum += $k;
}
echo $sum;
?>

A. 1
B. 2
C. 3
D. 4
E. 5 © 2008 Haim Michael
The array_sum Function

Given the following scrip what will be the output?

<?php
$varA = array(2,2,1,2,1,1,2,2,2,3,3,4);
$varB = array_count_values($varA);
$sum = array_sum($varB);
echo $sum;
?>

A. 3
B. 4
C. 10
D. 11
E. 12

© 2008 Haim Michael


Comparing Arrays
What is the output of the following code?

<?php
//$a = ['a'=>'avodado','b'=>'bamba','c'=>'calco'];
//$b = ['b'=>'bamba','a'=>'avodado','c'=>'calco'];
$a = [123,455,323];
$b = [323,123,455];
if($a==$b)
{
echo "bamba";
}
else
{
echo "bisli";
}
?>

_______

© 2008 Haim Michael


The list Construct
What is the output of the following code?

<?php
$vec = [2=>"dave",1=>"ron",0=>"chen",3=>"ran"];
list($a,$b,$c) = $vec;
echo "<br>a=$a";
echo "<br>b=$b";
echo "<br>c=$c";
?>

________

© 2008 Haim Michael


The foreach Loop
What is the output of the following code?

<?
$sum = 0;
$vec = [12,5,4];
foreach($vec as $k=>$v)
{
$sum += $k;
}
echo $sum;
?>

___

© 2008 Haim Michael


Textual Keys
What is the output of the following code?

<?
$vec = ['a'=>'abba','i'=>'is','w'=>'awesome'];
echo $vec['a'].' '.$vec['i'].' '.$vec['w'];
?>

________

© 2008 Haim Michael


The usort Function

Complete the missing code in order to sort the students according to their
average.
<?php
class Student
{
private $id;
private $average;
private $name;
function __construct($idVal, $averageVal, $nameVal)
{
$this->id = $idVal;
$this->average = $averageVal;
$this->name = $nameVal;
}

public function getId()


{
return $this->id;
}

© 2008 Haim Michael


The usort Function
public function getAverage()
{
return $this->average;
}

public function getName()


{
return $this->name;
}
public function __toString()
{
return $this->getName () . " id=" . $this->getId () . "
average=" . $this->getAverage ();
}
}

© 2008 Haim Michael


The usort Function
$vec = [
new Student ( 123123, 98, "danidin" ),
new Student ( 523434, 88, "moshe" ),
new Student ( 456544, 92, "spiderman" ),
new Student ( 744565, 77, "superman" )
];

usort($vec, function ($a, $b)


{
echo "<br>comparing between ".$a->getName()." and ".$b->getName();
return _________________;
} );

echo "<h2>after</h2>";
foreach ( $vec as $k => $v )
{
echo "<Br>$k => " . $v;
}

?>

© 2008 Haim Michael


The String Tricky Array Key
What is the output of the following code?

<?php
$vec = array('5'=>"abc",5=>"fggh");
$temp = '5';
echo $vec[$temp];
?>

© 2008 Haim Michael


Tricky foreach By Reference
What is the output of the following code?

<?php
$sum = 0;
$vec = array(1,2,3,4,5);
foreach($vec as $key => &$val)
{
$sum += $val;
$val = 9;
}
$sum -= 15;
for($i=1;$i<=3;$i++)
{
$sum += $vec[$i];
}
echo $sum;
?>

© 2008 Haim Michael


Type of Array Keys
What are the possible types of array keys?

_________

© 2008 Haim Michael


Tricky Sum of Array Values
What is the output of the following code?

<?php
$vec = array (2, 2, 4, 7, 8, 9, 12, 14);
$sum = 0;
for ($i = 0; $i < 3; $i++)
{
$sum += $vec[$vec[$i]];
}
echo $sum;
?>

_________

© 2008 Haim Michael


Tricky List Construct
What is the output of the following code?

<?php
$vec = array(0=>"david",2=>"moshe",1=>"michael");
list($a,$b,$c) = $vec;
echo $b;
?>

_________

© 2008 Haim Michael


© 2008 Haim Michael 12/12/13

Arrays

© 2008 Haim Michael

© 2008 Haim Michael 1


© 2008 Haim Michael 12/12/13

Constants & Arrays


What will the following script output?

<?php
define(index, "5");
$vec[5] = "BOMBO";
$vec['conto'] = "DONGO";
$vec["index"] = "CONGO";
print "The value is: ";
print $vec[index]."\n";
?>

A. BOMBO
B. DONGO
C. CONGO
D. 0
E. 5

© 2008 Haim Michael

Answer:
A

Explanation:
The value of the index constant is “5”. Accessing an array with the “5” key results in
treating that key as if it was the simple 5 integer number. Therefore, whether trying to
access $vec[“5”] or $vec[5] we will eventually get the same value.

© 2008 Haim Michael 2


© 2008 Haim Michael 12/12/13

Strings & Arrays


What will the following script output?

<?php
$vec='abcdefghijklmnopqrstuvwxyz';
echo $vec[7].$vec[0].$vec[8].$vec[12];
?>

A. haim
B. ibjn
C. aaaa
D. hhhh
E. mmmm

© 2008 Haim Michael

Answer:
A

Explanation:
When having a variable that holds a string value we can treat it as if it was an array and
access each one of the characters.

© 2008 Haim Michael 3


© 2008 Haim Michael 12/12/13

Avoid The Index


What will the following script output?

<?php
$vec= array();
$vec[]=4;
$vec[]=12;
$vec[]=32;
$sum=$vec[0]+$vec[1]+$vec[2];
echo $sum;
?>

A. 48
B. 0
C. Null
D. 16
E. 44

© 2008 Haim Michael

Answer:
A

Explanation:
When trying to assign a value into an array the index in use is the default one (according
to the index of the last added element).

© 2008 Haim Michael 4


© 2008 Haim Michael 12/12/13

Associative Arrays
Associative arrays use _____ keys.

A. string
B. int
C. float
D. objects
E. compound

© 2008 Haim Michael

Answer:
A

Explanation:
Associative arrays use strings as their keys. The following is an example for an
associative array:
$vec = array(“en“=>“English“,“he“=>“Hebrew“,“ru“=>“Russian“)

© 2008 Haim Michael 5


© 2008 Haim Michael 12/12/13

Array of Arrays
Given the following script which of the options reference “david” in a correct way?

<?php
$matrix = array
(
"en"=>"English",
"he"=>"Hebrew",
"ru"=>"Russian",
"borochov"=>array("Gogoya",1600,2008),
"green"=>array("john"=>array("moshe","david","salam"))
);
?>

A. $matrix["green"]["john"][1];
B. $matrix["green"]["john"][2];
C. $matrix[4][0][1];
D. $matrix[4][1][1];
E. $matrix[4][0][2];
© 2008 Haim Michael

Answer:
A

Explanation:
The word “david” is the second element of the array associated as a value with “john”
key, as an array associated as a value with “green” key.

© 2008 Haim Michael 6


© 2008 Haim Michael 12/12/13

Arrays For Each Loop


Given the following script what will be the output?

<?php
$sum = 0;
$vec = array(3,3);
foreach($vec as $key => &$val)
{
$sum += $val;
$val = 4;
}
foreach($vec as $key => $val)
$sum += $val;
echo $sum;
?>

A. 14
B. 12
C. Compilation Error
D. Compilation Warning
© 2008 Haim Michael

Answer:
A

Explanation:
The first loop changes each one of the array values from 3 into 4.

© 2008 Haim Michael 7


© 2008 Haim Michael 12/12/13

The serialize Function

Given the following script what will be the output?

<?php
$sum = 0;
$vec = array(100,90,80);
$ser = serialize($vec);
$arr = unserialize($ser);
foreach($arr as $key => $value)
{
$sum += $value;
}
echo $sum;
?>

A. 270
B. 0
C. Compilation Error
D. Compilation Warning
E. Null
© 2008 Haim Michael

Answer:
A

Explanation:
The serialize translates an array into a string representation. The unserialize translates a
string representation back to its original array.

© 2008 Haim Michael 8


© 2008 Haim Michael 12/12/13

The Native Sort


Given the following script what will be the output?

<?php
$vec = array('a12','a1','a2','a24');
natsort($vec);
foreach($vec as $val)
{
echo "$val ";
}
?>

A. a1 a2 a12 a24
B. a1 a12 a2 a24
C. a24 a12 a2 a1
D. Compilation Warning
E. Null

© 2008 Haim Michael

Answer:
A

Explanation:
The natsort function sorts the elements in their natural order.

© 2008 Haim Michael 9


© 2008 Haim Michael 12/12/13

No Key
Given the following scrip what will be the output?

<?php
$array = array ('a'=>12,'6'=>9,1,8);
$sum = $array[6]+$array[7];
echo $sum;
?>

A. 10
B. 18
C. 19
D. Compilation Warning
E. Null

© 2008 Haim Michael

Answer:
A

Explanation:
The PHP starts assigning numeric keys to elements without a hard-coded key from the
lowest numeric key available. If a numeric key to start with wasn't set it starts from zero.

© 2008 Haim Michael 10


© 2008 Haim Michael 12/12/13

The array_intersect Function

Given the following script what will be the output?

<?php
$sum = 0;
$varA = array(2,4,6,3,12);
$varB = array(4,6,9,3);
$varC = array(3,2,6);
$varD = array_intersect($varA,$varB,$varC);
foreach($varD as $k => $v)
{
$sum += $k;
}
echo $sum;
?>

A. 1
B. 2
C. 3
D. 4
© 2008 Haim Michael
E. 5

Answer:
E

Explanation:
The array_intersect function traverses each one of the values in the first array and
includes it within the resulted array if it is included within the values of each one of the
other arrays. The keys remain the original keys in the first array. The resulted array in this
sample includes two values: 6 and 3. The key of 6 is 2 and the key of 3 is 3. The sum of 2
and 3 gives 5.

© 2008 Haim Michael 11


© 2008 Haim Michael 12/12/13

The array_sum Function

Given the following scrip what will be the output?

<?php
$varA = array(2,2,1,2,1,1,2,2);
$varB = array_count_values($varA);
$sum = array_sum($varB);
echo $sum;
?>

A. 3
B. 4
C. 5
D. 6
E. 8

© 2008 Haim Michael

Answer:
E

Explanation:
The array_count_values calculates the number of occurrences of each one of the values.
In this case the returned array includes two values: 3 and 5. The value 2 is shown 5
times. The value 1 is shown 3.

© 2008 Haim Michael 12


© 2008 Haim Michael 12/12/13

Float Values Cannot Be Keys


Given the following scrip what will be the output?

<?php
$array = array (1.112 => 'a', 1.2 => 'b', 1.9 => 'c');
echo count ($array);
?>

A. 1
B. 2
C. 3
D. 0
E. Null

© 2008 Haim Michael

Answer:
A

Explanation:
Keys can be either integers or strings. They cannot be floats. Therefore, each one of the
float values is changed into an integer one. The result is an array with one element only.
The key is 1 and the value is 'c'.

© 2008 Haim Michael 13


© 2008 Haim Michael 12/12/13

Tricky Sum
Given the following scrip what will be the output?

<?php
$vec = array (1, 2, 3, 5, 8, 9);
$sum = 0;
for ($i = 0; $i < 3; $i++)
{
$sum += $vec[$vec[$i]];
}
echo $sum;
?>

A. 10
B. 6
C. 11
D. 12
E. Null

© 2008 Haim Michael

Answer:
A

Explanation:
The sum calculation is of the values in the following indexes 1,2 and 3. 2+3+5=10.

© 2008 Haim Michael 14


© 2008 Haim Michael 12/12/13

The + Operator
Given the following scrip what will be the output?

<?php
$vecA = array(2,1,1,4,2);
$vecB = array(3,2,1,8,3,1);
$vecC = $vecA + $vecB;
$sum = array_sum($vecC);
echo $sum;
?>

A. 10
B. 6
C. 11
D. 12
E. Null

© 2008 Haim Michael

Answer:
C

Explanation:
The sum calculation is of 2,1,1,4,2 & 1, which gives 11.

© 2008 Haim Michael 15


© 2008 Haim Michael 12/12/13

The === Operator


Given the following scrip what will be the output?

<?php
$vecA = array(2,1,1,4,2);
$vecB = array(1,2,1,4,2);
$vecC = array(1,2,1,4,2);
$sum = 0;
if($vecA==$vecB) $sum++;
if($vecA===$vecC) $sum++;
if($vecB===$vecC) $sum++;
echo $sum;
?>

A. 0
B. 1
C. 2
D. 3
E. Null
© 2008 Haim Michael

Answer:
B

Explanation:
The == returns true if the two arrays have the same elements (meaning, the same values
and key). For that reason $vecA is not == with $vecB. The === returns true if the two
arrays have the same elements (meaning, the same values and keys) and their position
is the same. For that reaons, $vecB is === with $vecC and $vecA is not === with $vecC.

© 2008 Haim Michael 16


© 2008 Haim Michael 12/12/13

The count Function 1/2

Given the following scrip what will be the output?

<?php
$vecA = array(2,1,4,2);
$vecB = array(1,2,1);
$vecC = array(1,2);
$matrix = array();
$matrix[0] = $vecA;
$matrix[1] = $vecB;
$matrix[2] = $vecC;
$sum = 0;
foreach($matrix as $val)
{
$sum += count($val);
}
echo $sum;
?>

© 2008 Haim Michael

© 2008 Haim Michael 17


© 2008 Haim Michael 12/12/13

The count Function 2/2

A. 9
B. 5
C. 3
D. 2
E. Null

© 2008 Haim Michael

Answer:
A

Explanation:
The count function returns the number of elements a given array has. This code sums the
total number of all elements in all arrays. Therefore, the result is 9.

© 2008 Haim Michael 18


© 2008 Haim Michael 12/12/13

The isset Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages = array(
"en"=>"English",
"he"=>"Hebrew",
"ru"=>"Russian",
"ro"=>"Romanian");
$vecLetters = array(
"a"=>"AAA",
"b"=>"BBB",
"c"=>"CCC",
"d"=>"DDD");

© 2008 Haim Michael

© 2008 Haim Michael 19


© 2008 Haim Michael 12/12/13

The isset Function 2/2


if(isset($vecNumbers['2']) && isset($vecLanguages['ru']))
$sum++;
if(isset($vecLetters['a']) &&
isset($vecNumbers[$vecNumbers[3]]))
$sum++;
if(!isset($vecLanguages['de'])) $sum++;
echo $sum;
?>

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael

Answer:
B

Explanation:
The three conditions are true. The result is 3. The $vecNumbers[$vecNumbers[3]]
is a bit tricky. It evaluates as $vecNumbers[2].

© 2008 Haim Michael 20


© 2008 Haim Michael 12/12/13

The array_key_exists Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages =
array("en"=>"English","he"=>"Hebrew","ru"=>"Russian","ro"=>"
Romanian");
$vecLetters =
array("a"=>"AAA","b"=>"BBB","c"=>"CCC","d"=>"DDD");
If( array_key_exists('2',$vecNumbers) &&
array_key_exists('ru',$vecLanguages)) $sum++;
If( array_key_exists('a',$vecLetters) &&
array_key_exists($vecNumbers[3],$vecNumbers)) $sum++;
if(!array_key_exists('de',$vecLanguages))
$sum++;
echo $sum;
?>
© 2008 Haim Michael

© 2008 Haim Michael 21


© 2008 Haim Michael 12/12/13

The array_key_exists Function 2/2

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael

Answer:
B

Explanation:
The three conditions are true. The result is 3. The $vecNumbers[3] is a bit tricky. It
evaluates as 2.

© 2008 Haim Michael 22


© 2008 Haim Michael 12/12/13

The in_array Function 1/2

Given the following code what will be the output?

<?php
$sum = 0;
$vecNumbers = array(2,1,4,2);
$vecLanguages = array(
"en"=>"English", "he"=>"Hebrew",
"ru"=>"Russian","ro"=>"Romanian");
$vecLetters =
array("a"=>"AAA","b"=>"BBB","c"=>"CCC","d"=>"DDD");
if(in_array("English",$vecLanguages)) $sum++;
if(in_array("AAA",$vecLetters)) $sum++;
if(in_array("EEE",$vecLetters)) $sum++;
if(in_array('4',$vecNumbers)) $sum++;
echo $sum;
?>

© 2008 Haim Michael

© 2008 Haim Michael 23


© 2008 Haim Michael 12/12/13

The in_array Function 2/2

A. 2
B. 3
C. 4
D. 1
E. 0

© 2008 Haim Michael

Answer:
B

Explanation:
The only condition that results as false is in_array("EEE",$vecLetters). All others are true.

© 2008 Haim Michael 24


© 2008 Haim Michael 12/12/13

The sort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
sort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael

Answer:
A

Explanation:
Calling sort results in sorting the array and assigning new key to each one of the
elements. For that reason, the old keys are no longer relevant.

© 2008 Haim Michael 25


© 2008 Haim Michael 12/12/13

The asort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
asort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael

Answer:
B

Explanation:
Calling asort results in sorting the array and keeping its keys.

© 2008 Haim Michael 26


© 2008 Haim Michael 12/12/13

The rsort Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,2=>3,0=>4,4=>22);
rsort($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael

Answer:
A

Explanation:
Calling rsort results in sorting the array in a reverse order and assigning new key to each
one of the elements. For that reason, the old keys are no longer relevant.

© 2008 Haim Michael 27


© 2008 Haim Michael 12/12/13

The shuffle Function

Given the following code what will be the output?

<?php
$sum = 0;
$vec = array(1=>45,8=>3,0=>4,5=>22);
shuffle($vec);
foreach($vec as $k=>$v)
{
$sum+=$k;
}
echo $sum;
?>

A. 6
B. 7
C. 8
D. 0
E. Null
© 2008 Haim Michael

Answer:
A

Explanation:
The shuffle function scrambles the elements and assign new keys for each one of them.
The new keys in this case are: 0,1,2 & 3. Their total gives 6.

© 2008 Haim Michael 28


© 2008 Haim Michael 12/12/13

The foreach Loop

Given the following code what will be the output?

<?php
$vec=array(1,2,3,4);
foreach($vec as $k=>$v)
{
$v=8;
}
$sum=array_sum($vec);
echo $sum;
?>

A. 6
B. 7
C. 8
D. 10
E. 32

© 2008 Haim Michael

Answer:
D

Explanation:
The foreach loop iterates a copy of the original array. The original array doesn't change.

© 2008 Haim Michael 29


© 2008 Haim Michael 12/12/13

The sort Function

What will be the output of the following code?

<?php
$vec=array(5,2,3,4);
$temp=sort($vec);
var_dump($temp);
?>

A. bool(true)
B. 1,2,3,4
C. Null
D. 1
E. Compilation Warning

© 2008 Haim Michael

Answer:
A

Explanation:
The sort function returns 'true' when it succeeds in its work. The 'true' value is assigned
into $temp. Calling var_dump in order to print out the value of a given variable that holds
'true' results in 'bool(true)'. Therefore, answer A is the correct one.

© 2008 Haim Michael 30


© 2008 Haim Michael 12/12/13

Array Elements Assignment


What will be the output of the following code?

<?php
$vec = array();
$vec[] = "italy";
$vec[] = "russia";
$vec[] = "canada";
$str;
foreach($vec as $k=>$v)
$str = $str." ".$v;
echo $str;
?>

A. italy russia canada


B. russia
C. Null
D. empty string
E. canada
© 2008 Haim Michael

Answer:
A

Explanation:
When trying to assign a value into an array without specifying the index number the value
will be assign in the next available place. For that reason, the loop assigns the three
strings into locations indexed with the following numbers: 0, 1 and 2.

© 2008 Haim Michael 31


© 2008 Haim Michael 12/12/13

Simple Native Sort


What will be the output of the following code?

<?php
$vec = array("pix14.png", "pix10.png", "pix3.png",
"pix1.png");
natsort($vec);
foreach($vec as $k=>$v)
{
echo $v." ";
}
?>

A. pix1.png pix3.png pix10.png pix14.png


B. pix3.png pix1.png pix10.png pix14.png
C. Null
D. empty string
E. pix14.png pix3.png pix10.png pix1.png

© 2008 Haim Michael

Answer:
A

Explanation:
When sorting an array using the natsort function we shall get a sort in according with the
user expectation.

© 2008 Haim Michael 32


© 2008 Haim Michael 12/12/13

The array_intersect Function

Given the following scrip what will be the output?

<?php
$sum = 0;
$varA = array('a','b',6,32,'asd');
$varB = array('b',6,9,3);
$varC = array(3,2,6,'asd');
$varD = array_intersect($varA,$varB,$varC);
foreach($varD as $k => $v)
{
$sum += $k;
}
echo $sum;
?>

A. 1
B. 2
C. 3
D. 4
© 2008 Haim Michael
E. 5

Answer:
B

Explanation:
The array_intersect function traverses each one of the values in the first array and
includes it within the resulted array if it is included within the values of each one of the
other arrays. The keys remain the original keys in the first array. The resulted array in this
sample includes one value: 6. The key of 6 is 2.

© 2008 Haim Michael 33


© 2008 Haim Michael 12/12/13

The array_sum Function

Given the following scrip what will be the output?

<?php
$varA = array(2,2,1,2,1,1,2,2,2,3,3,4);
$varB = array_count_values($varA);
$sum = array_sum($varB);
echo $sum;
?>

A. 3
B. 4
C. 10
D. 11
E. 12

© 2008 Haim Michael

Answer:
E

Explanation:
The array_count_values calculates the number of occurrences of each one of the values
and returns an array that its keys are the values of the original array and their values are
the number of times each one of them occurs.

© 2008 Haim Michael 34


© 2008 Haim Michael 12/12/13

Comparing Arrays
What is the output of the following code?

<?php
//$a = ['a'=>'avodado','b'=>'bamba','c'=>'calco'];
//$b = ['b'=>'bamba','a'=>'avodado','c'=>'calco'];
$a = [123,455,323];
$b = [323,123,455];
if($a==$b)
{
echo "bamba";
}
else
{
echo "bisli";
}
?>

_______

© 2008 Haim Michael

Answer:
bisli

Explanation:
The arrays have different elements (e.g. 0=>123 in $a and 1=>123 in $b).

© 2008 Haim Michael 35


© 2008 Haim Michael 12/12/13

The list Construct


What is the output of the following code?

<?php
$vec = [2=>"dave",1=>"ron",0=>"chen",3=>"ran"];
list($a,$b,$c) = $vec;
echo "<br>a=$a";
echo "<br>b=$b";
echo "<br>c=$c";
?>

________

© 2008 Haim Michael

Answer:
chen
ron
dave

Explanation:
The list construct assigns the array values in accordance with the keys. The variable 'a' is
assigned with the value that its key is 0, the variable 'b' is assigned with the value that its
key is '1' and the variable 'c' is assigned with the value that its key is 2.

© 2008 Haim Michael 36


© 2008 Haim Michael 12/12/13

The foreach Loop


What is the output of the following code?

<?
$sum = 0;
$vec = [12,5,4];
foreach($vec as $k=>$v)
{
$sum += $k;
}
echo $sum;
?>

___

© 2008 Haim Michael

Answer:
3

Explanation:
This foreach loop sums the keys (not the values). The total of 0, 1 and 2 is 3.

© 2008 Haim Michael 37


© 2008 Haim Michael 12/12/13

Textual Keys
What is the output of the following code?

<?
$vec = ['a'=>'abba','i'=>'is','w'=>'awesome'];
echo $vec['a'].' '.$vec['i'].' '.$vec['w'];
?>

________

© 2008 Haim Michael

Answer:
abba is awesome

Explanation:
The keys can be strings. Referring 'a' gives 'abba', referring 'I' gives 'is' and referring 'a'
gives 'awesome'.

© 2008 Haim Michael 38


© 2008 Haim Michael 12/12/13

The usort Function

Complete the missing code in order to sort the students according to their
average.
<?php
class Student
{
private $id;
private $average;
private $name;
function __construct($idVal, $averageVal, $nameVal)
{
$this->id = $idVal;
$this->average = $averageVal;
$this->name = $nameVal;
}

public function getId()


{
return $this->id;
}

© 2008 Haim Michael

© 2008 Haim Michael 39


© 2008 Haim Michael 12/12/13

The usort Function


public function getAverage()
{
return $this->average;
}

public function getName()


{
return $this->name;
}
public function __toString()
{
return $this->getName () . " id=" . $this->getId () . "
average=" . $this->getAverage ();
}
}

© 2008 Haim Michael

© 2008 Haim Michael 40


© 2008 Haim Michael 12/12/13

The usort Function


$vec = [
new Student ( 123123, 98, "danidin" ),
new Student ( 523434, 88, "moshe" ),
new Student ( 456544, 92, "spiderman" ),
new Student ( 744565, 77, "superman" )
];

usort($vec, function ($a, $b)


{
echo "<br>comparing between ".$a->getName()." and ".$b->getName();
return _________________;
} );

echo "<h2>after</h2>";
foreach ( $vec as $k => $v )
{
echo "<Br>$k => " . $v;
}

?>

© 2008 Haim Michael

Answer:
$a->getAverage() - $b->getAverage()

Explanation:
The usort function should receive two arguments. The first argument is the array we want
to sort. The second argument is the function that we want usort to invoke again and again
in order to complete the sort. That function should be with two parameters. The usort
function passes over two values (in this code sample these two values are two
references for two objects) and that function should return 0 (if the two values are in the
same position), a value bigger than 0 (if the first value should be first) and a value smaller
than 0 (if the second value should be first).

© 2008 Haim Michael 41


© 2008 Haim Michael 12/12/13

The String Tricky Array Key


What is the output of the following code?

<?php
$vec = array('5'=>"abc",5=>"fggh");
$temp = '5';
echo $vec[$temp];
?>

© 2008 Haim Michael

Answer:
fggh

Explanation:
When using a key which is a string that evaluates to number it would be the same as if
we were using the number directly. Therefore, accessing the array using '5' or 5 is the
same. In this code sample the “fggh” value overrides “abc”.

© 2008 Haim Michael 42


© 2008 Haim Michael 12/12/13

Tricky foreach By Reference


What is the output of the following code?

<?php
$sum = 0;
$vec = array(1,2,3,4,5);
foreach($vec as $key => &$val)
{
$sum += $val;
$val = 9;
}
$sum -= 15;
for($i=1;$i<=3;$i++)
{
$sum += $vec[$i];
}
echo $sum;
?>

© 2008 Haim Michael

Answer:
27

Explanation:
When iterating an array using foreach and having the $val marked by reference using & it
means that every change we introduce into $val will be a change in our array.

© 2008 Haim Michael 43


© 2008 Haim Michael 12/12/13

Type of Array Keys


What are the possible types of array keys?

_________

© 2008 Haim Michael

Answer:
string, int

Explanation:
Keys of arrays can be of the types int and string only.

© 2008 Haim Michael 44


© 2008 Haim Michael 12/12/13

Tricky Sum of Array Values


What is the output of the following code?

<?php
$vec = array (2, 2, 4, 7, 8, 9, 12, 14);
$sum = 0;
for ($i = 0; $i < 3; $i++)
{
$sum += $vec[$vec[$i]];
}
echo $sum;
?>

_________

© 2008 Haim Michael

Answer:
16

Explanation:
The calculation is of $vec[2] + $vec[2] + $vec[4] = 16.

© 2008 Haim Michael 45


© 2008 Haim Michael 12/12/13

Tricky List Construct


What is the output of the following code?

<?php
$vec = array(0=>"david",2=>"moshe",1=>"michael");
list($a,$b,$c) = $vec;
echo $b;
?>

_________

© 2008 Haim Michael

Answer:
michael

Explanation:
The list construct works in accordance with the keys. The $b is assigned with the value of
the key 1.

© 2008 Haim Michael 46

You might also like