PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Nodral
Forum Newbie
Posts: 10 Joined: Fri Sep 17, 2010 7:10 am
Post
by Nodral » Fri Sep 17, 2010 9:30 am
Ah, the old Unexpected T_variable chesnut!!!
Apparently on line 24 of this code. I'm stumped!!!
Any ideas guys?
Code: Select all
<?php
function percentCalc($value1,$value2,$value3,$value4,$value5,$total)
{
$array=array($value1,$value2,$value3,$value4,$value5);
while (list($key,$arraycontent)= each($array))
{
$c_percent=(($arraycontent/$total)*100);
$final_array[]=$c_percent;
}
return $final_array;
}
function CleanArray($array)
{
foreach($array as $value)
{
$pattern='/\w/';
if ($value == $pattern)
{
$arrayout[]=($value);
}
}
reset $arrayout
return $arrayout;
}
?>
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Fri Sep 17, 2010 10:01 am
This line is missing a semicolon.
Nodral
Forum Newbie
Posts: 10 Joined: Fri Sep 17, 2010 7:10 am
Post
by Nodral » Fri Sep 17, 2010 10:15 am
Done that, Still same error
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Fri Sep 17, 2010 10:25 am
Reset is a function. It needs parentheses.
Why do you need to reset the array here?
Nodral
Forum Newbie
Posts: 10 Joined: Fri Sep 17, 2010 7:10 am
Post
by Nodral » Fri Sep 17, 2010 1:06 pm
I was taught to reset as good habit.
It seems that $arrayout is not an array. I can guarentee there ave values coming in, but I need to filter out any which are not letters or numbers, hence the function, is there an issue with the if statement?
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Fri Sep 17, 2010 2:04 pm
As long as you are gathering habits... Always initialize variables. For example,
This line compares two strings. It does not perform a regular expression match.
To filter an array using a regular expression, use
preg_grep() . Your entire CleanArray() function can be replaced by a call to preg_grep().
Nodral
Forum Newbie
Posts: 10 Joined: Fri Sep 17, 2010 7:10 am
Post
by Nodral » Mon Sep 20, 2010 3:37 am
Ok, now using the preg_grep function instead of the user defined function I built. However, now I get this error. I can't find a comma in the wrong place in this script. Any ideas? Apparently there is an unexpected , on line 4.
Sorry to be a pain and a complete noob, but this has had me scrathcing my head!!!
Code: Select all
<?php
function percentCalc($value1, $value2, $value3, $value4, $value5, $total)
{
$array[]=array($value1,$value2,$value3,$value4,$value5);
while (list($key,$arraycontent)= each($array))
{
$c_percent=(($arraycontent/$total)*100);
$final_array[]=$c_percent;
}
return $final_array;
}
?>
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Mon Sep 20, 2010 11:38 am
Compare these two scripts. The first has a one-dimensional array with three elements. The other has a two-dimensional array with one element.
Code: Select all
$array = array('a', 'b', 'c');
print_r(each($array));
/*
Array
(
[1] => a
[value] => a
[0] => 0
[key] => 0
)
*/Code: Select all
$array[] = array('a', 'b', 'c');
print_r(each($array));
/*
Array
(
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[value] => Array
(
[0] => a
[1] => b
[2] => c
)
[0] => 0
[key] => 0
)
*/
Why not have a single parameter for percentCalc() -- an array of values?
Is $total the sum of all the values? If so, that calculation can occur inside the function (see array_sum()).