Count up from one var to another - php

Hey guys a quick question in php.
This probably is pretty simple I'm not sure but I would like to know how to count up from one variable to another. So say I have the value 5 and the value 9 in the other I would like the PHP code to count each number and store in a array.
e.g
$var1 = 5;
$var2 = 9;
it should store each value (5,6,7,8,9) in a array so i can access them like so arrayname[1]
Thanks in advance

This should do it for you
$var1 = 5;
$var2 = 9;
foreach (range($var1, $var2) as $number) {
echo $number;
}
If you want to store it as an array then
$number = range($var1, $var2);

Another alternative:
$var1 = 5;
$var2 = 9;
$numbers = array();
for ( $i=$var1; $i <= $var2; $i++ ) {
$numbers[] = $i;
}
Then the variable $numbers should be the array that you want.

Related

is it posible to auto increment variable name after a while loop?

I need to create a php file with a hundred variables, which are all identical except for their id.
PHP Code
$var1 = get_input('myvar1');
$var2 = get_input('myvar2');
$var3 = get_input('myvar3');
$var4 = get_input('myvar4');
...
$var30 = get_input('myvar30');
I wonder if it is possible to create only one line as a model, and is replicated 30 times?
I think you are looking after something like this :
$vars = [];
for($i = 1; $i <= 30; $i++) {
$vars[] = get_input('myvar' . $i);
}
this is a job for arrays
$var = array_fill(1, 30, 'myvar');
use the array key as your "id"
Why bother with arrays when you can create the variables like so. The variables names are also set dynamically just like the values.
for ($i = 1; $i <= 100; $i++) {
${'var' . $i} = get_input('myvar' . $i);
}

Iterate on PHP variable (list/array) then output the index based on its value

Using the following example in PHP:
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
1) I would like to iterate on the 4 values in $priv. Would 'foreach' be the correct way to do it?
2) If the value is higher than a given number, I would like to echo the index of this value. Not sure how to do it. The comparaison must be INT (not string).
Ex. using "30" it would output:
PAGE_C
PAGE_D
Is it possible? Or maybe I am not using the correct container for what I'm trying to do ?
PS. How would you call the type of "$priv" in this example ? An array ? An indexed variable ? A dictionary ? A list ?
Thank you.
basically:
<?php
function foo($var){
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
$out='';
foreach ($priv as $k=>$v){
if ($v >$var){
$out .= $k.'<br>';
}
}
return $out;
}
echo foo('30');
demo: http://codepad.viper-7.com/GNX7Gf
Just create an array with the letters to iterate over.
$letters = array('A','B','C','D');
for($i=0;$i<count($letters);$i++) {
if($priv['PAGE_' . $letters[$i]] > /*value*/) {
echo $priv['PAGE_' . $letters[$i]];
}
}
$priv is an array.
Also, it's not too clear to me what you are exactly trying to do. Are you trying to echo the value of the array element if it's greater than a constant value?
We could do it using PHP builtin array functions. Its good to use builtin functions if possible in case of performance.
array_walk will do the trick for you. In this case $priv is an associative PHP array. Following is the one line script that will do what you want to achieve:
$input = 30;
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
array_walk($priv, function($value, $key, $input){ if($value > $input) echo $key . '<br>';}, $input);

How to call variables sharing the same name but ending with different numbers by assigning a variable to these numbers

I couldn't find a better title for this problem. I have variables like this: $var1, $var2, $var3.
In a for loop, I want to display these variables:
for ( $j = 1; $j <= 3; $j++ ) {
echo $var$j;
}
Of course this won't work, but what is the syntax to do it? If $var1 = 1, $var2 = 2 and $var3 = 3, I want this result: "123".
Use curly braced syntax (known as variable variables):
for ( $j = 1; $j <= 3; $j++ ) {
echo ${"var$j"};
}
Also, as #Alnitak mentioned in comment:
if you're using numbers to handle a set of variables like this, you should have used an array in the first place.
So array is definately an option, as it might be exact case for it's usage.
This is how to solve your problem:
How to put a value inside :
// $content is an array of values.
for ( $j = 1; $j <= 3; $j++ ) {
$var_name = "var".$j;
$$var_name = $content[$j];
}
And how to read them :
for ( $j = 1; $j <= 3; $j++ ) {
$var_name = "var".$j;
echo $$var_name;
}
But may I suggest to use arrays:
// $content is an array of values.
foreach ( $content as $key=>$value) {
echo $content[$key];
echo $value; //same as previous line
unset($content[$key]);//to remove a value from an array
}
If you can, switch to using arrays instead of these variables. If you can't, you can still use an array:
foreach (array($var1, $var2, $var3) as $current) {
echo $current;
}
I think one should use an Array instead of lots of unnecessary variables.
Keeping things structured, as what is held here is a Data Structure at all.
One may implode/join them using an empty value as is:
$values = [1,2,3];
echo join('', $values);
As simple as that! Think of it!
By the sake of portability, there may be the case where an Associative Array is used, let's say when the data is fetched from a Database Rowset.
As of PHP has a way to extract array values only, one can use:
$values = [
'value1' => 1,
'value2' => 2,
'value3' => 3
];
echo join('', array_values($values));
And, finally, considering the previous associative concept, it is also possible to use object properties:
$obj = new \stdClass();
$obj->value1 = 1;
$obj->value2 = 2;
$obj->value3 = 3;
echo join('', array_values(get_object_vars($obj)));
This will work for you:
$var1 = "1";
$var2= "2";
$var3 = "3";
for ( $j = 1; $j <= 3; $j++ ) {
echo ${"var".$j};
}
and the output will be
123
The syntax is
${"firstPart".$lastPart};
which is equal to
$firstPartlastPart
You may read about it more here: http://www.php.net/manual/en/language.variables.variable.php

Given an array of integers, what's the most efficient way to get the number of other integers in the array within n?

Given the following array:
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
And assuming $n = 2, what is the most efficient way to get a count of each value in the array within $n of each value?
For example, 6 has 3 other values within $n: 5,7,7.
Ultimately I'd like a corresponding array with simply the counts within $n, like so:
// 0,0,1,2,2,5,6,7,7,9,10,10 // $arr, so you can see it lined up
$count_arr = array(4,4,4,4,4,3,3,4,4,4, 2, 2);
Is a simple foreach loop the way to go? CodePad Link
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
$range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
$count = count(array_intersect($arr,$range)); // count intersect array
$count_arr[] = $count-1; // subtract 1 so you don't count itself
}
print_r($arr);
print_r($count_arr);
My last answer was written without fully groking the problem...
Try sorting the array, before processing it, and leverage that when you run through it. This has a better runtime complexity.
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
asort($arr);
$n = 2;
$cnt = count($arr);
$counts = array_pad(array(), $cnt, 0);
for ($x=0; $x<$cnt; $x++) {
$low = $x - 1;
$lower_range_bound = $arr[$x]-$n;
while($low >= 0 && ($arr[$low] >= $lower_range_bound)) {
$counts[$x]++;
$low--;
}
$high = $x + 1;
$upper_range_bound = $arr[$x]+$n;
while($high < $cnt && $arr[$high] <= $upper_range_bound) {
$counts[$x]++;
$high++;
}
}
print_r($arr);
print_r($counts);
Play with it here: http://codepad.org/JXlZNCxW

use variable name in code processing php like $number01 use it value, not name

so better will be example (so hard to give a title to this problem)
imagine I have variables in php like
$number1 = 10;
$number2 = 30;
$number3 = 23;
.. and so on..
and I just want to make an arithmetic average but not like
($number1 + $number2 +$number3)/3 because it is so much typing (having more than only 3 numbers)
but use something like
$temp=0;
for ($i=1;$i<50;$i++){
$temp=$temp+$number."$i"; <- this is what I don't know how to define..
}
and similar, how to echo all values for numbers like
for ($i=1;$i<50;$i++){
echo $number.$i; <- but this is not working..
}
I hope I have described it good, thank you for your help!
It can be done with variable variables but it's ugly:
$temp=0;
for ($i=1;$i<50;$i++){
$temp=$temp+(${$number.$i});
}
There is a limitation with the variable variables which is you don't know how high the number goes. You would also need to check if the variable is set first. A better solution is to store each of the variables in an array, which you can easily iterate over:
$arr = array();
$arr[] = 10;
$arr[] = 30;
$arr[] = 23;
$temp = 0;
foreach($arr as $val)
{
$temp += $val;
}
$number = array(10,20,30);
echo array_sum($number);
the other solution doing the same with for loop
$number = array(10,20,30);
$count = count($number);
$sum = 0;
for($i = 0; $i < $count ; ++$i) $sum += $number[$i];
or foreach
$number = array(10,20,30);
$count = count($number);
$sum = 0;
foreach($number as $n) $sum += $n;
there are a lot of methods to add items to array for example
$number = array(10,20,30);
$number = array();
$number[] = 10; $number[] = 20; $number[] = 30;
$number[0]= 10; $number[1] = 20; $number[2] = 30;
$number = array();
array_push($number, 10, 20, 30);
But the most simple and working solution is to use arrays and array_sum() function.
Array sum - Calculate the sum of values in an array
It's probably better to put all these values into an array instead of different variables. This makes adding later easier and allows you to loop like:
foreach($myArray as $number){
echo $number;
}
If you still want/need to use variables you can use variable variables:
$temp=0;
for ($i=1;$i<50;$i++){
$numberName = "number".$i;
$temp=$temp + $$numberName;//Note the $$ syntax.
}
$$foo will be the value of the variable with the name stored in $foo.
A final note. Be careful with variable variables, as they can lead to messy or unreadable code.
use an array
$number[0] = 10;
$number[1] = 20;
$number[2] = 23;
for ($i=1;$i<count($number);$i++) {
echo $number[$i]; //now this works.
}

Categories