Define array with variable index [closed] - php

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 8 years ago.
Improve this question
My code:
$rank_content = file('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2');
$line_count = 0;
//initializing only the first few keys because of no reason (the latter ones aren't in use yet)
$rankNameArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
$rankRlmpArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
while ($line = array_shift($rank_content)) { // retrieving line after line of the website, does work indeed
$line_count += 1;
if(strpos($line, "Warrior #") || strpos($line, "Archer #") || strpos($line, "Mage #"))
{
$rankNameArr[$line_count] = $line_count + 1; // HERE nothing happens
$rankRlmpArr[$line_count] = $line_count + 2; // nothing happens here, too
}
}
Why does
echo $rankNameArr[2];
echo $rankRlmpArr[2];
give me the value 42 instead of the correct value? If I replace $line_count with a real number, the script works properly.
My intention is to store the value $line_count + 1 into $rankNameArr at position $line_count. Not very complicated actually
EDIT -------------
Forget everything above, please. I finally reduced the script to the actual problem:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$arr[$counter]=$i;
}
echo $arr[5];
This sadly echoes 42. I have no idea how to have $arr[$counter] store the actual value of $i.

I have no idea how to have $arr[$counter] store the actual value of $i.
$counter is set to 0, so $arr[$counter] is the same as saying $arr[0]. If you echo $arr[0] you'll see that it is being changed while the rest are left alone, which means your code is working.
However if you want $counter to increment also, you just need to tell it to do so:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$counter++;
$arr[$counter]=$i;
}
echo $arr[5];

Related

How to echo same sentence random times in php? [closed]

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 6 years ago.
Improve this question
I need to implement this method to my webpage. I want to echo the same sentence random times every time someone refresh my php webpage.
For example: Hello World!
You can do:
<?php
$min = 1; // Minimum 1 time
$max = 10; // Maximum 10 times
$string = "Hello world!\n"; // Print Hello World! and a new line
$x = rand($min,$max); // Make the random
$i = 0; // Set the iterator
do {
echo $string; // Echo the text
$i = $i + 1; // Increment the iterator
} while ($i < $x); // Test to make sure we didn't do it too many times
?>
This echos "Hello world!" anywhere from 1 to 10 times. You can search the PHP API to see how this works.

for loop - print values in order - PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to print the below statement.
Thanks for updating your PH_NUMBER, ADDRESS, OFFICE NUMBER and EMAIL.
Where PH_NUMBER, ADDRESS, OFFICE_NUMBER and EMAIL are variables.
For example, if value is empty for variable "OFFICE_NUMBER" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and EMAIL.
For example, if value is empty for variable "EMAIL" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and OFFICE NUMBER.
$a = "Address";
$b = "Mobile";
$c = "email";
$array = array($a, $b, $c);
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$total = $array[$i];
}
echo "Thanks for udpating " . $total ;
How can this be done ?
Any suggestions would be helpful.
You don't need to use a loop.
<?php
/* Collect all parameters to the array */
$variables = [
'PH_NUMBER',
'ADDRESS',
'EMAIL',
'OFFICE_NUMBER',
];
/* Sorting the array if need */
/* Pop the element off the end of array */
$variables = array_filter($variables);
$count = count($variables);
$lastVariable = array_pop($variables);
if($count > 1) {
printf('Thanks for updating your %s and %s', implode(', ', $variables), $lastVariable);
}elseif($count === 1){
printf('Thanks for updating your %s', $lastVariable);
}else{
print 'Nothing for update';
}
?>
Update: Small improvement in order to catch cases with only 1 variable and empty array (nothing for update).

PHP function to get N random values (as array) which's sum result in X [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I think the subject hits it better than I was expecting.
I need a function which returns random numbers which result in a given value (X) when they are summed up.
Something like this:
getRandomTo(10); // result for example: array(2,3,5)
getRandomTo(10); // result for example: array(4,3,3)
getRandomTo(12); // result for example: array(5,1,6)
I could not find a generic algorithm function for solving that requirement. Further more I cannot imagine a FAST and performant way to create something like this my self.
Please help me out
function getRandomTo($num)
{
$x = Array();
$i = 0;
do
{
$x[$i] = rand(1,$num);
$num = $num - $x[$i];
$i++;
}while($num > 0);
print_r($x);
}
Maybe do this:
create a random value between 0 and X; say this one is called r1. save this in your array.
create another random value between 0 and (X-r1), name it r2. save it also.
do these steps as often as you need it (or as long as r1+...+rn is lower than X)
Another solution:
function randomTo($numIn) {
$numOut = 0;
$numbers = array();
do {
$add = rand(1, $numIn);
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
} while( $numOut != $numIn );
return $numbers;
}
$result = randomTo(15);
var_dump($result);

Append a number to every php instance [closed]

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 9 years ago.
Improve this question
I have a form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post variable, but I need to add the recipient,s area code on front of every number, I have tried to explode the variable using the comma as a separator but I do not know what to do from there
$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
I do not know how you're getting the area codes in the script, but to prepend the number with the area code, you can just use string concatenation, like so:
$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
$result[] = $areacode . '_' . $numbers[$i];
}
This can also be done using a foreach:
foreach (explode(',', $recipient) as $number) {
$result[] = $areacode . '_' . $number;
}
Now, if you want to get this back as a comma-separated string, you can use implode(), like so:
$result_string = implode(',', $result);
The short answer is something like:
$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
$numbers_with_area_codes[] = '+372'.$numbers[$i];
}

How to implement for loop logic [closed]

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 9 years ago.
Improve this question
Is there a simple logic to implement for loop, demending on delimeter?
if $a>b for($i=a; $i>b; $i--)
else for($i=a; $i<b; $i++)
But I need it to be done in one loop I cant do something like
$start = $a<$b? $a : $b;
Because I need loop to always start with $a and go toward $b, maybe there another way?Can i have reason for downvoting? And what is unclear in my question, peoples if you dont understand question refrain from touching it.
Hope this helps
$inc = $a < $b ? 1: -1;
for ($i = $a; $i != $b; $i += $inc) {
/* some code here */
}
Explanation:
First step is estimation of the increment meaning whether to increment or decrement $a to reach $b.
Obviously if $a < $b then increment needed or else decrement. $i += $inc is the generic statement that adds +1 or -1. Adding +1 is increment and adding -1 is decrement.
Ultimately the loop exit condition is $i != $b, hoping this condition will be met atleast once in the increment/decrement.
Your question contains some logical error. Although syntax is correct.
if $a<b for($i=a; $i>b; $i--)
Here, $a is smaller than b and in for loop you give condition $i>b which will never meet because $i and $a are already smaller than b. So, this loop will not run even for a single time. This is also same for else because here also loop will not execute for a single time.
Is that you want..?

Categories