Get the last for loop variable value - php

I want to display the last modified variable outside the for loop
Code:
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
}
**echo $pm_discussion;**
For example:
$i inside the for loop having values from 1 to 6.
Then it should display $pm_discussion = $_POST['pm_discussion'.$i];
The above $i should be 6.

Try following code.
$temp='';
for( $i = 1; $i <= 100; $i++ )
{
$pm_discussion = $_POST['pm_discussion'.$i];
$pm_update = $_POST['pm_update'.$i];
$pm_reports = $_POST['pm_reports'.$i];
$pm_informed = $_POST['pm_informed'.$i];
$pm_complete = $_POST['pm_complete'.$i];
if($pm_discussion!='')
$temp = $pm_discussion;
}
echo $temp;

Try this, it will alsays store the last non empty value to the variable -
for( $i = 1; $i <= 100; $i++ )
{
if (!empty($_POST['pm_discussion'.$i])) {
$pm_discussion = $_POST['pm_discussion'.$i];
}
if (!empty($_POST['pm_update'.$i])) {
$pm_update = $_POST['pm_update'.$i];
}
if (!empty($_POST['pm_reports'.$i])) {
$pm_reports = $_POST['pm_reports'.$i];
}
if (!empty($_POST['pm_informed'.$i])) {
$pm_informed = $_POST['pm_informed'.$i];
}
if (!empty($_POST['pm_complete'.$i])) {
$pm_complete = $_POST['pm_complete'.$i];
}
}
echo $pm_discussion;

Related

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

How to optimize code that multiplies every row and column in multidimensional array?

I'm trying to find a way to optimize my code, and I have difficulties in finding a right way. First I'm adding numbers in empty array and after that, I'm multiplying every row and column using pure logic, without any php function that could help me out. For now I've tried to calculate rows and columns in one nested for loop, but it doesn't work.
$nxmArr = [];
$rows = 4;
$cols = 4;
$value = 1;
for( $row = 0; $row < $rows; $row++ )
{
for( $col = 0; $col < $cols; $col++ )
{
$nxmArr[$row][$col] = $value++;
}
}
for( $row = 0; $row < $rows; $row++ )
{
$colSum = 1;
for( $col = 0; $col < $cols; $col++ )
{
echo $nxmArr[$row][$col]." ";
$colSum *= $nxmArr[$row][$col];
}
echo "Col Sum: $colSum<br>";
}
echo "<br>";
for( $col = 0; $col < $cols; $col++ )
{
$rowSum = 1;
for( $row = 0; $row < $rows; $row++ )
{
echo $nxmArr[$row][$col]." ";
$rowSum *= $nxmArr[$row][$col];
}
echo "Row Sum: $rowSum<br>";
}
You can approach this by using
$nxmArr = [];
$rows = 4;
$cols = 4;
$value = 1;
$flag = 0;
for( $row = 0; $row < $rows; $row++ )
{
for( $col = 0; $col < $cols; $col++ )
{
$nxmArr[$row][$col] = $value++;
$flag += $nxmArr[$row][$col];
($row > 0)
?
($colns[$col] += $nxmArr[$row][$col])
:
($colns[$col] = $nxmArr[$row][$col])
;
}
$rowSum[] = $flag;
$flag = 0;
}
echo 'Columns Sum :-';
print_r($colns);
echo '<hr>';
echo 'Rows sum :-';
print_r($rowSum);
Live Demo :- https://3v4l.org/d3ml4
Explanation :-
$flag += $nxmArr[$row][$col]; This will do the row elements addition one by one and when the single row traversed, assign sum to an array element i.e. $rowSum[] = $flag; and make the flag element 0 i.e. $flag=0
($row > 0)
?
($colns[$col] += $nxmArr[$row][$col])
:
($colns[$col] = $nxmArr[$row][$col])
When the $row=0, the code ($colns[$col] = $nxmArr[$row][$col]) will be executes and it will keep the values like $colns[0]=1,$colns[1]=2,$coln[2]=3,$colns[3]=4, Now when $row>0 , it will add the new values to indices 0,1,2,3

how to generate dynamic number array like below

$big_box = array(1,2,3,16,17,18,31,32,33,46,47,48,61,62,63,76,77,78,91,92,93,106,107,108,121,122,123,136,137,138............);
$small_box = array(4,19,34,49,64.............);
I want to generate dynamic numbers array like above example upto 10000.
For Array $big_box Try to something like this.
$j = 0;
for ($i=0; $i <= 1000; $i++) {
if ($i!=0) {
$big_box[] = $i;
$j++;
}
if ($j == 3) {
$j = 1;
$big_box[] = $i+13;
$i =$i+13;
}
}
echo "<pre>";
print_r($big_box);
And array $small_box
$small_box = array();
for($i=0; $i <= 1000; $i++) {
if ($i == 0) {
$small_box[$i] = 4;
}else{
$small_box[$i] = $small_box[$i-1]+15;
}
}
echo "<pre>";
print_r($small_box);

php merging or concatenation strings when assigning a variable

PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1

defining alphabets as numbers not working inside loop

Please check my code below,it returns 0 while I am expecting a result 14.But when I add A+D manually it returns 5.Am i doing something wrong inside the loop ?
<?php
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
for($i = 0; $i<=$len; $i++)
{
$val += $name[$i];
}
echo $val; //returns 0
?>
You need to use constant(..) to get the value of a constant by name. Try this:
for ($i = 0; $i < strlen($name); $i++) {
$val += constant($name[$i]);
}
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
$val = null;
for($i = 0; $i<=$len-1; $i++)
{
$val += constant($name[$i]);
}
echo $val;

Categories