I have this code:
// Values of drop-down lists (cmbPosition)
$studPosition = array();
$i = 0;
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition[$i] = $_GET[$value];
$i++;
// Calculates the points for each student in the event
$points = ($_SESSION['noStudents']+1) - $_GET[$value];
echo $points;
$_SESSION['points'] = $points;
}
The code above loops through $_SESSION['arrayNamePosition'] which contains an array. Every thing works but $_SESSION['points'] = $points; is where the problem is (This is the $_SESSION variable which contains $points).
When I echo $points in the current php form, it outputs: 583276
But when I echo $_SESSION['points'] inside a while loop in a different php form it only outputs the last element stored in $echo $_SESSION['points']points which is 6.
How can I fix this so that echo $_SESSION['points'] outputs all the values of $points in a different php form.
NOTE: I have also put echo $_SESSION['points'] inside a for loop but it still outputs the last value stored in $points. e.g. output: 666666
Thanks in advance.
It's because $_SESSION['points'] itself is not an array. You should change your line to:
$_SESSION['points'][] = $points;
Variable points is not an array, therefore it contains only last calculated element. Change it to array, then save each value as new element in array.
I guess you mean that the loop with echo $points; outputs you 583276. Like the $_SESSION['points'], it stores only one value, but you print the value each time.
I would suggest following changes:
$studPosition = array();
$i = 0;
$points = '';
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition[$i] = $_GET[$value];
$i++;
$points = $points.(($_SESSION['noStudents']+1) - $_GET[$value]);
}
echo $points;
$_SESSION['points'] = $points;
Related
I'm rather new to programming in general. I'm starting off with some exercises but I'm kind of getting stuck. I created an array and looped thru with a foreach loop to print out each individual number stored in the array, but I dont know what to do to find the average of the numbers and print it out.
<?php
$myArray = array(87,75,93,95);
foreach($myArray as $value){
echo "$value <br>";
}
?>
only as an exercise, becuse if you actully wanted to do this you would use #kittykittybangbang's answer
<?php
$myArray = array(87,75,93,95);
$sum='';//create our variable
foreach($myArray as $value){
$sum+=$value; //adds $value to $sum
//echo "$value <br>";
}
echo $sum;
?>
for the count count($myArray); makes the most senses but you could do that in the loop as well:
<?php
$myArray = array(87,75,93,95);
$sum= $count=0;// initiate interger variables
foreach($myArray as $value){
$sum+=$value; //adds $value to $sum
$count++; //add 1 on every loop
}
echo $sum;
echo $count;
//the basic math for any average:
echo $sum/$count;
?>
if you don't create $sum and $count before the loop you would get notices returned from php, as the first time it tried to add to either, there would be noting to add to
You could:
$avg = array_sum($myArray) / count($myArray);
echo $avg;
Where:
array_sum calculates the sum of all elements in the given array.
count outputs the total number of elements in the given array.
I asked a similar question earlier but I couldn't get a clear answer to my issue. I have a function "isParent" that gets 2 pieces of data. Each 1 of the 2 gets a string separating each value with a , or it just gets a plain int and then checks if the first value given is a parent of the second.
I pull the 2 bits of data in and explode them but when I go through my nested for loop and try to test
$toss = $arr1[$i];
print_r($toss);
It comes up blank. I have no idea what the issue is: Here is the full code of the function...
function isParent($parent, $child)
{
$parentArr = explode(',', $parent);
$childArr = explode(',',$child);
//Explode by Comma here. If array length of EITHER parentArr or childArr > 1 Then throw to an Else
if(count($parentArr) <= 1 && count($childArr) <= 1) //If explode of either is > 1 then ELSE
{
$loop = get_highest_slot(15);
for($i = $loop; $i > 0; $i--)
{
$temp = get_membership_from_slot($i,'id_parent','id_child');
if($temp['id_parent'] == $parent && $temp['id_child'] == $child)
{
return 1;
}
}
}
else //set up a for loop in here so that you traverse each parentArr value and for each iteration check all child values
{
$i = count($parentArr);
$c = count($childArr);
for(;$i >=0;$i--) //Loop through every parent
{
for(;$c >=0;$c--)
{
echo '<br>$i = ';
print_r($i);
echo '<br><br>Parent Arr at $i:';
$toss = $parentArr[$i];
echo $toss;
echo '<br>';
print_r($childArr);
echo '<br><br>';
if(isParent($parentArr[$i],$childArr[$c])) //THIS CAUSES AN INFINITE YES! Learn how to pull an array from slot
{
return 1;
}
}
}
}
return 0;
}
You are missing some code for the slot procedures. Apart from that, you probably need to use a different variable for the inner for loop. because $c will be 0 after the first iteration of $i.
Thanks for the help! The issue was in the recursive call back to the top of the function. It was tossed empty slots and when comparing 2 empty slots it returned a false positive. A quick !empty() check fixed it.
I have an array of about 100 different random number like this:
$numbers=array(10,9,5,12, ..... .... ... ...);
now i want to make an array of random numbers from this array so that addition of selected numbers will be my given number. example: i may ask to get array of numbers such that, if i add all numbers it will be 100.
i am trying to do it in this way,
function rendom_num ($array,$addition)
{
//here is the code
}
print_r (rendome_num ($numbers,100));
i am not able to fiend the code for last 3 days!
Please use shuffle-
<?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
}
?>
php.net
can use shuffle as #chatfun said or can try array_rand if want only some random values from your array
$value= array("Rabin","Reid","Cris","KVJ","John");
$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];
Something like this should work. The breakdown is commented so you know what it's all doing.
function Randomizer($number = 100)
{
// This just generates a 100 number array from 1 to 100
for($i=1; $i <= 100; $i++) {
$array[] = $i;
}
// Shuffles the above array (you may already have this array made so you would need to input into this function)
shuffle($array);
// Assign 0 as base sum
$sum = 0;
// Go through the array and add up values
foreach($array as $value) {
// If the sum is not the input value and is also less, continue
if($sum !== $number && $sum < $number) {
// Check that the sum and value are not greater than the input
if(($sum + $value) <= $number) {
// If not, then add
$sum += $value;
$new[] = $value;
}
}
// Return the array when value hit
else
return $new;
}
// If the loop goes through to the end without a successful addition
// Try it all again until it does.
if($sum !== $number)
return Randomizer($number);
}
// Initialize function
$test = Randomizer(100);
echo '<pre>';
// Total (for testing)
echo array_sum($test);
// Array of random values
print_r($test);
echo '</pre>';
I want to retrieve the last element in an array from $max and reference it in the array $set_of_path. I want only the last value. The problem with this code is this $max value who prints value 0 to end of file, thus instead of output the last value .. every element is implemented.. Can someone help me please. thanks in advance.
for($ind=0;$ind <count($array_file[0]['pair']);$ind++) {
....
...
if((($city)===$initial or ($city[0]===$initial))){
$x =$array_file[0]['pair'][$ind]['city'];//display all cities
if ((end($x))===$final){
//push $array_file content into the array
$xd[]=$array_file[0]['pair'][$ind];
$count=count(end($xd));
$last_element = array();
$a[] = array_merge($xd);
$end = count(end($a));
$push = array_push($last_element,$end);
$max = max($last_element)-1;
print_r($max);
$set_of_path=#$a[$max];
print_r($set_of_path);
print_r('<pre>');
}
};
...
...
.
...
.
..
Use SizeOf for getting the count of the items in an array.
http://php.net/manual/en/function.sizeof.php
Solution
<?php
$i = 0;
//Process array in loop
//increment $i++; or $i = sizeof($array);
?>
I have an array declared above the beginning of a for loop as: $array = array();.
Now, in the for loop I start inserting values into it.
At some point I make one of its index as another array as $array[$j]=array();
And insert some values like, $array[$j][$l] = id; and so on.
Now, when I use print_r ($array); inside the loop I get the expected value of the array.
But outside the loop this newly created array (2-D) is getting lost and I am getting only a 1-D array as an output.
Can someone please tell me where the problem could lie?
The following code works properly. Perhaps you are switching your variables as strager suggests.
<?php
$array = array();
for ($i = 0; $i < 10; $i+=1) {
if ($i == 5) {
$array[$i] = array('value 1', 'value 2');
} else {
$array[$i] = $i;
}
}
print_r($array);
?>