Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
How do you sort this empty array using foreach loop to output these 25 numbers in order?
Example outcome would be: 1 1 1 1 1 2 2 2 2 3 3 3 3 3 4 4 4 4
<?php
$array = array();
for($counter = 0; $counter <=25; $counter++){
$die = rand(1, 10);
$int[$counter] = $die;
echo " $die ";
}
sort($die);
foreach ($die as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
?>
<?php
$array = array();
for ($i = 0; $i <=25; $i++) {
echo $array[] = rand(1, 10);
}
sort($array);
foreach ($array as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track - I'm using PHP code.
This my code :
<? php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
echo $total = $rp-$v; ?>
my output should be:
10000
9000
7500
7000
5000
1000
Is this what you want?
<?php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
$rp = $rp - $v;
$count--;
//echo $count;
echo $rp."<br/>" ;
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help to get the position of duplicate or same elements in an array.
For example
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
Result
6 = 0,7
5 = 1,11
3 = 2,8
7 = 3,6,14
40 = 4,11,15
45 =5 and so on.
One simple approach:
<?php
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
$pos = array();
foreach($arr as $k => $v) {
$pos[$v][] = $k;
}
foreach($pos as $k => $v) {
echo $k."=".implode(',', $v)."<br>";
}
?>
Result:
6=0,7
5=1,12
3=2,8,13
7=3,6,14
40=4,11,15
45=5
32=9
86=10
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have following code:
$source_stat_array = array(0 => 'clicks', 1 => 'impr', 2 => 'spend');
$count = count($source_stat_array);
for ($i = 0; $i < count; $i++) {
echo $source_stat_array[$i++];
}
the result i get is following:
clicks spend
instead of i need
clicks impr spend
can you answer me what is wrong with code?
You are incrementing the value of $i twice.
Try the below code:
for ($i = 0; $i < $count; $i++) {
echo $source_stat_array[$i];
}
You can use foreach it will iterate through each key
foreach($source_stat_array as $key => $value){
echo $value;
}
Read more about foreach , manual
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to loop through $i which starts from 790000000 and echo every 100000 up to 799999999:
790000000
790100000
790200000
790300000
790400000
...
I tried this code but it didn't work:
for ($i=790000000; $i<=800000000; $i+100000) {
echo $i . '<br>';
}
For loop wrong code, you are missing actual code update as $i+100000 does not update variable. Use $i += 100000 instead.
// Here is problem in your code
for ($i=790000000; $i<=800000000; $i += 100000) {
echo $i . '<br>';
}
Update your loop using += instead of i.
Only using + will not increment your $i variable value.
for ($i=790000000; $i<=800000000; $i+=100000) {
echo $i . '<br>';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)