Arrays
Arrays
<?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
<?php
$vec='abcdefghijklmnopqrstuvwxyz';
echo $vec[7].$vec[0].$vec[8].$vec[12];
?>
A. haim
B. ibjn
C. aaaa
D. hhhh
E. mmmm
<?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
A. string
B. int
C. float
D. objects
E. compound
<?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
<?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
<?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
<?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
<?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
<?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
<?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
<?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
<?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
<?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;
?>
A. 9
B. 5
C. 3
D. 2
E. Null
<?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");
A. 2
B. 3
C. 4
D. 1
E. 0
<?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
<?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;
?>
A. 2
B. 3
C. 4
D. 1
E. 0
<?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
<?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
<?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
<?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
<?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
<?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
<?php
$vec = array();
$vec[] = "italy";
$vec[] = "russia";
$vec[] = "canada";
$str;
foreach($vec as $k=>$v)
$str = $str." ".$v;
echo $str;
?>
<?php
$vec = array("pix14.png", "pix10.png", "pix3.png",
"pix1.png");
natsort($vec);
foreach($vec as $k=>$v)
{
echo $v." ";
}
?>
<?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
<?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
<?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";
}
?>
_______
<?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";
?>
________
<?
$sum = 0;
$vec = [12,5,4];
foreach($vec as $k=>$v)
{
$sum += $k;
}
echo $sum;
?>
___
<?
$vec = ['a'=>'abba','i'=>'is','w'=>'awesome'];
echo $vec['a'].' '.$vec['i'].' '.$vec['w'];
?>
________
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;
}
echo "<h2>after</h2>";
foreach ( $vec as $k => $v )
{
echo "<Br>$k => " . $v;
}
?>
<?php
$vec = array('5'=>"abc",5=>"fggh");
$temp = '5';
echo $vec[$temp];
?>
<?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;
?>
_________
<?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;
?>
_________
<?php
$vec = array(0=>"david",2=>"moshe",1=>"michael");
list($a,$b,$c) = $vec;
echo $b;
?>
_________
Arrays
<?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
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.
<?php
$vec='abcdefghijklmnopqrstuvwxyz';
echo $vec[7].$vec[0].$vec[8].$vec[12];
?>
A. haim
B. ibjn
C. aaaa
D. hhhh
E. mmmm
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.
<?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
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).
Associative Arrays
Associative arrays use _____ keys.
A. string
B. int
C. float
D. objects
E. compound
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“)
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.
<?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.
<?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.
<?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
Answer:
A
Explanation:
The natsort function sorts the elements in their natural order.
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
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.
<?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.
<?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
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.
<?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
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'.
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
Answer:
A
Explanation:
The sum calculation is of the values in the following indexes 1,2 and 3. 2+3+5=10.
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
Answer:
C
Explanation:
The sum calculation is of 2,1,1,4,2 & 1, which gives 11.
<?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.
<?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;
?>
A. 9
B. 5
C. 3
D. 2
E. Null
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.
<?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");
A. 2
B. 3
C. 4
D. 1
E. 0
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].
<?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
A. 2
B. 3
C. 4
D. 1
E. 0
Answer:
B
Explanation:
The three conditions are true. The result is 3. The $vecNumbers[3] is a bit tricky. It
evaluates as 2.
<?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;
?>
A. 2
B. 3
C. 4
D. 1
E. 0
Answer:
B
Explanation:
The only condition that results as false is in_array("EEE",$vecLetters). All others are true.
<?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.
<?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.
<?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.
<?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.
<?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
Answer:
D
Explanation:
The foreach loop iterates a copy of the original array. The original array doesn't change.
<?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
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.
<?php
$vec = array();
$vec[] = "italy";
$vec[] = "russia";
$vec[] = "canada";
$str;
foreach($vec as $k=>$v)
$str = $str." ".$v;
echo $str;
?>
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.
<?php
$vec = array("pix14.png", "pix10.png", "pix3.png",
"pix1.png");
natsort($vec);
foreach($vec as $k=>$v)
{
echo $v." ";
}
?>
Answer:
A
Explanation:
When sorting an array using the natsort function we shall get a sort in according with the
user expectation.
<?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.
<?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
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.
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";
}
?>
_______
Answer:
bisli
Explanation:
The arrays have different elements (e.g. 0=>123 in $a and 1=>123 in $b).
<?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";
?>
________
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.
<?
$sum = 0;
$vec = [12,5,4];
foreach($vec as $k=>$v)
{
$sum += $k;
}
echo $sum;
?>
___
Answer:
3
Explanation:
This foreach loop sums the keys (not the values). The total of 0, 1 and 2 is 3.
Textual Keys
What is the output of the following code?
<?
$vec = ['a'=>'abba','i'=>'is','w'=>'awesome'];
echo $vec['a'].' '.$vec['i'].' '.$vec['w'];
?>
________
Answer:
abba is awesome
Explanation:
The keys can be strings. Referring 'a' gives 'abba', referring 'I' gives 'is' and referring 'a'
gives 'awesome'.
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;
}
echo "<h2>after</h2>";
foreach ( $vec as $k => $v )
{
echo "<Br>$k => " . $v;
}
?>
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).
<?php
$vec = array('5'=>"abc",5=>"fggh");
$temp = '5';
echo $vec[$temp];
?>
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”.
<?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;
?>
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.
_________
Answer:
string, int
Explanation:
Keys of arrays can be of the types int and string only.
<?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;
?>
_________
Answer:
16
Explanation:
The calculation is of $vec[2] + $vec[2] + $vec[4] = 16.
<?php
$vec = array(0=>"david",2=>"moshe",1=>"michael");
list($a,$b,$c) = $vec;
echo $b;
?>
_________
Answer:
michael
Explanation:
The list construct works in accordance with the keys. The $b is assigned with the value of
the key 1.