i have starting date ..
i need to add months in starting date inside for loop
for ex -
startingdate = 25-03-2015
and my loop runs two time then it generate like below
25-04-2015
25-05-2015
thats it...
$startingdate = $_POST['startingdate'];
$dates = date("d-m-Y", strtotime($startingdate . " +" . " 1MONTHS -1 DAYS"));
for ($i = 0; $i <= 2; $i++)
{
$coupondate = $dates;
}
If your results are returned with commas separting. Then use explode() to return an array of values. Then using max() and min() in php you can set your lower and upper limits to loop through.
But if you're getting all the results anyway, explode() should do the trick, then just loop through them.
$result = "100,101,102,103";
$arr = explode(",", $result);
foreach ($arr as $number) {
echo $number;
}
Or with min / max:
$result = "100,101,102,103";
$arr = explode(",", $result);
$min = min($arr);
$max = max($arr);
for ($i = $min; $i <= $max; $i++) {
echo $i;
}
Take a look at
min() and
max()
$points=array(500,501,502,503,504,505,506);
$min=min($points);
$max=max($points);
for($i=$min;$i<$max+1;++$i{
//do something
}
If your data is stored lower to upper :
$text = '500,501,502,503,504,505,506';
$explode = explode(',',$text);
$index = count($explode)-1;
for ($i=$explode[0]; $i<=$explode[$index]; $i++)
{
echo $i;
}
If order can change, add :
sort($explode);
Try this :
//after connected with a database
$data = (explode(",", $rows["points"]));
for ($i=min($data); $i<=max($data); $i++)
{
}
Note : If the separation between words is ', ' (a comma and space) then change with this :
$data = (explode(", ", $rows["points"]));
Related
I'm trying to stop $implode_demographics inside for loop below.
For example, if I use echo $implode_demographics inside loop, then I will get:
a01a01,a02a01,a02,a03a01,a02,a03,a04a01,a02,a03,a04,a05a01,a02,a03,a04,a05,a06a01,a02,a03,a04,a05,a06,a07a01,a02,a03,a04,a05,a06,a07,a08a01,a02,a03,a04,a05,a06,a07,a08,a09a01,a02,a03,a04,a05,a06,a07,a08,a09,a10a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02a01,a02,a....
But If I use this string outside, then output works.
a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02,b03,c01,c02,c03,c04,c05,c06,c07,c08,c09,c10,c11,c12,c13
Online PHP test
So, how I can echo $implode_demographics inside for loop to get same result?
$a_l_demographics = [11,3,13];
$a_p_demographics = ['a','b','c'];
$a_r_demographics = [];
$c_demographics_values = array();
$a_c_demographics = min(count($a_l_demographics), count($a_p_demographics));
for ($i = 0; $i < $a_c_demographics; $i++) {
for ($j = 1; $j <= $a_l_demographics[$i]; $j++) {
$a_r_demographics[] = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
$implode_demographics = implode($a_r_demographics, ',');
// this won't work
echo $implode_demographics;
}
}
// this works
// echo $implode_demographics;
It doesn't works because you implode the same array again and again after you add data in it.
So in your first echo you have only a01, then you implode after with a01 and a02 value, etc.
Try to add echo $implode_demographics . "\n"; and you will see that you have one var, then two, then three, etc. see example here : https://3v4l.org/WrCgJ
So if you want to echo each value INSIDE, just do :
<?php
$a_l_demographics = [11,3,13];
$a_p_demographics = ['a','b','c'];
$a_r_demographics = [];
$c_demographics_values = array();
$a_c_demographics = min(count($a_l_demographics), count($a_p_demographics));
for ($i = 0; $i < $a_c_demographics; $i++) {
for ($j = 1; $j <= $a_l_demographics[$i]; $j++) {
$a_r_demographics[] = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
$implode_demographics = implode($a_r_demographics, ',');
// You echo only the last value
echo $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
// If it's not the last loop : you add a `,`
if (!($i == ($a_c_demographics - 1) && $j == $a_l_demographics[$i]))
echo ",";
}
}
This way you will echo only the LAST value each time and not all the value from the beginning each time, the output is :
a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02,b03,c01,c02,c03,c04,c05,c06,c07,c08,c09,c10,c11,c12,c13
See code here : https://3v4l.org/MEnGe
In fact it's work, you have a problem with the implode. Implode d'ont put the glue (,) to the last value !
try this.
$implode_demographics = implode($a_r_demographics, ',');
echo $implode_demographics.', ';
i have a set of arrays:
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
what i want to do is to get a set of $data array from each number of $nums array,
the output must be:
output:
2=11,22
3=33,44,55
1=66
what i tried so far is to slice the array and remove the sliced values from an array but i didn't get the correct output.
for ($i=0; $i < count($nums); $i++) {
$a = array_slice($data,0,$nums[$i]);
for ($x=0; $x < $nums[$i]; $x++) {
unset($data[0]);
}
}
Another alternative is to use another flavor array_splice, it basically takes the array based on the offset that you inputted. It already takes care of the unsetting part since it already removes the portion that you selected.
$out = array();
foreach ($nums as $n) {
$remove = array_splice($data, 0, $n);
$out[] = $remove;
echo $n . '=' . implode(',', $remove), "\n";
}
// since nums has 2, 3, 1, what it does is, each iteration, take 2, take 3, take 1
Sample Output
Also you could do an alternative and have no function usage at all. You'd need another loop though, just save / record the last index so that you know where to start the next num extraction:
$last = 0; // recorder
$cnt = count($data);
for ($i = 0; $i < count($nums); $i++) {
$n = $nums[$i];
echo $n . '=';
for ($h = 0; $h < $n; $h++) {
echo $data[$last] . ', ';
$last++;
}
echo "\n";
}
You can array_shift to remove the first element.
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
foreach( $nums as $num ){
$t = array();
for ( $x = $num; $x>0; $x-- ) $t[] = array_shift($data);
echo $num . " = " . implode(",",$t) . "<br />";
}
This will result to:
2 = 11,22
3 = 33,44,55
1 = 66
This is the easiest and the simplest way,
<?php
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
$startingPoint = 0;
echo "output:"."\n";
foreach($nums as $num){
$sliced_array = array_slice($data, $startingPoint, $num);
$startingPoint = $num;
echo $num."=".implode(",", $sliced_array)."\n";
}
?>
I want to parse and expand the given strings in PHP.
From
0605052&&-5&-7&-8
0605052&&-4&-7
0605050&&-2&-4&-6&-8
To
0605052, 0605053 ,0605054 ,0605055, 0605057, 0605058
0605052,0605053,0605054,0605057
0605050,0605051,0605052,0605054,0605056,0605058
can someone help me with that? thanks in advance!
Your question is not very clear, but I think you mean a solution like this:
Edited: Now the hole ranges were shown and not only the specified numbers.
<?php
$string = "0605052&&-5&-7&-8";
$test = '/^([0-9]+)\&+/';
preg_match($test, $string, $res);
if (isset($res[1]))
{
$nr = $res[1];
$test = '/\&\-([0-9])/';
preg_match_all($test, $string, $res);
$result[] = $nr;
$nrPart = substr($nr, 0, -1);
$firstPart = substr($nr, -1);
if (isset($res[1]))
{
foreach ($res[1] as &$value)
{
if ($firstPart !== false)
{
for ($i=$firstPart+1; $i<=$value; $i++)
{
$nr = $nrPart . $i;
$result[] = $nr;
}
$firstPart = false;
}
else
{
$nr = $nrPart . $value;
$result[] = $nr;
$firstPart = $value;
}
}
}
var_dump($result);
}
?>
This delivers:
result[0] = "0605052"
result[1] = "0605053"
result[2] = "0605054"
result[3] = "0605055"
result[4] = "0605057"
result[5] = "0605058"
I think a multi step approach is the best thing to do here.
E.g. take this as an example 0605052&&-5&-7&-8:
Split at -. The result will be 0605052&&, 5&, 7&, 8
The first result 0605052&& will help you create your base. Simply substring the numbers by finding first occurence of & and substring to the next to last number. Result will be 060505. You will also need the last number, so get it as well (which is 2 in this case).
Get the remaining ends now, all \d& are simple to get, simply take the first character of the string (or if those can be more than one number, use substring with first occurence of & approach again).
The last number is simple: it is 8.
Now you got all important values. You can generate your result:
The last number from 2., all numbers from 3. and the number from 4. together with your base are the first part. In addition, you need to generate all numbers from the last number of 2. and the first result of 3. in a loop by a step of 1 and append it to your base.
Example Code:
<?php
$str = '0605052&&-5&-7&-8';
$split = explode('-', $str);
$firstAmpBase = strpos($split[0], '&');
$base = substr($split[0], 0, $firstAmpBase - 1);
$firstEnd = substr($split[0], $firstAmpBase - 1, 1);
$ends = [];
$firstSingleNumber = substr($split[1], 0, strpos($split[1], '&'));
for ($i = $firstEnd; $i < $firstSingleNumber; $i++) {
array_push($ends, $i);
}
array_push($ends, $firstSingleNumber);
for ($i = 2; $i < count($split) - 1; $i++) {
array_push($ends, substr($split[$i], 0, strpos($split[$i], '&')));
}
array_push($ends, $split[count($split) - 1]);
foreach ($ends as $end) {
echo $base . $end . '<br>';
}
?>
Output:
0605052
0605053
0605054
0605055
0605057
0605058
I am trying to make card game.
I have 6 variable that are stored in array. Than I use fisherYates method to randomize array, and display four of them.
Problem is, when I randomize it this way only, it will give only random output of those six, with all different types.
So I want that some repeats like, if you draw four cards, you get output of
ex: club, club, diamond,heart, or heart, star,star,heart.. if you get a point..
I thought to do it like this way: put the array in loop of 4 times, and every time it loops, it stores first, or last value in new array, so that way, I can have greater chances of combination of same cards in output array.
But I'm stuck, and I don't know how to do it :/
this is what I've tried so far
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
reset($niz);
$i++;
} while ($i <= 3);
Got a solution. Was just to simply do foreach of first element of multidimensional array :)
Code goes like this:
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
$niz=array();
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
//reset($niz);
$i++;
} while ($i <= 3);
foreach ($niz as $key ) {
$randomArr[]=$key[0]; ;
}
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
print_r($randomArr);
How can I achieve number 4,7,8 separately if there is a given variable: $i=478 such that I can store $j=4,$k=7,$l=8 in PHP. Is there a function?
$i = 478;
$stri = (string)$i;
$j = $stri[0];
$k = $stri[1];
$l = $stri[2];
You should cast the number to a string and then str_split it:
$numbers = str_split((string)$number); // or
list($a, $b, $c) = str_split((string)$number); // but this might throw a notice if $number is 12 or 8 or something
Use type case and convert number to string
$i=478 ;
$i= (string)$i ;
String can be accessed as array hence you can do
echo $i[0]; //to access 4
you can iterate as array as well using loops
$len=strlen($i);
for($j=0; $j <= $len; $j++)
{
echo $i[$j]."<br>";
}
try
$n=567;
$x = (string)$n;
for($i=0;$i<strlen($x);$i++)
{
echo "<br/>".$x[$i];
}
UPDATE:
to find the average
for($i=0;$i<strlen($x);$i++)
{
echo "<br/>".$x{$i};
$t+=(int)$x[$i];
}
echo "average:". $t/strlen($x);