function dataAnalysis Type function dataAnalysis in the box. Do not include PHP
ID: 3771272 • Letter: F
Question
function dataAnalysis
Type function dataAnalysis in the box. Do not include PHP tags:
function dataAnalysis($parameter1,$parameter2,$parameter3) {
// The parameter names can be whatever you want.
// Your code goes here.
};
OBJECTIVE
Write a function dataAnalysis() in PHP. The function totals values in one array based on values in the other array.
PARAMETERS
parameter 1 = a delimiter
parameter 2 = a delimited list of values
parameter 3 = a delimited list of values
WHAT TO DO
Explode data in parameter2 into a 1st array using parameter #1 as the delimiter
Explode data in parameter3 into a 2nd array using parameter #1 as the delimiter
Return the total of all values in the 2nd array for each parallel item in the 1st array where the value is: big
EXPLANATION
If array1[562] = "big" then add array2[562] to the total.
TIP
Name the arrays anything you want. NOT ALLOWED IN YOUR CODE echo
sum
average
4
5
6
7
8
9 CODE STRUCTURE Indent each code or comment line in a code block 5 spaces LINES PER COMMENT Each 3 lines of code require at least 1 comment line.
A comment is at least 20 characters.
Explanation / Answer
function dataAnalysis($delimiter, $list1, $list2) {
// reading delimited strings in to arrays using built-in function explode
$array1 = explode($delimiter, $list1);
$array2 = explode($delimiter, $list2);
//calculating the size od tha array
$arrSize = sizeof($array1);
// local variable to store the total value
$total = 0;
// for loop to iterate through all the elementa of the arrays
for($i = 0; $i < $arrSize; $i++)
{
if($array1[$i] == "big") // checking for "big" as array element in array1
$total += $array2[$i]; // add the corresponding element in array2 to the total
}
return $total;
};
If it helped you, please rate the answer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.