I want to ask pretty simple question (for most of you) but I can't find the solution right now.
We have $names = array('Alex','James','Jack');
I want to use for loop to echo this:
1. Alex
2. James
3. Jack
But I'm using this loop right now which isn't working as I want it to:
for($i = 0; $i <= count($names); $i++)
{ echo $i.$names[$i]."<br/>"; }
and it's echoing this:
0. Alex
1. James
2. Jack
The problem is because we are starting from 0 because it's an array. If I put a starting point 1 it's missing the first object from the array.
What's the fix for that?
Why not just increment your variable
$names = array('Alex','James','Jack');
for($i = 0; $i <= count($names); $i++) {
$j = $i+1;
echo $j.$names[$i]."<br/>";
}
Related
OK, simple question. New to PHP. How do I accomplish something similar to the following in PHP? This is no specific language intentionally.
i = 0;
j = 0;
k = 50;
While i <= 5
While j <= 10
myarray(i,j) = k
i = i + 1
j = j + 1
k = k + 2
next
next
I have found much info regarding arrays and loops in PHP and none seem to accomplish this simple task. Please don't be snarky... just give me a hand here.
You start each inner while j<=10.
Note that you didn't set $j to 0.
So correct the code is follows:
$i = 0;
$k = 50;
While( $i <= 5 ) {
$j = 0;
While( $j <= 10) {
$myarray[$i][$j] = $k;
$j++;
$k = $k + 2;
}
$i++;
}
Your pseudo code leaves some ambiguities, but maybe you mean to do the following:
$k=50;
for ($i=0; $i<=5; $i++)
for ($j=0; $j<=10; $j++, $k+=2)
$myarray[$i][$j]=$k;
This is a nested for-loop. The actual assignment to $myarray will happen 6*11=66 times in total and $k will have values from 50 to 115.
In PHP arrays don't need to be declared. You can simply assign a value to an arbitrary index of a given variable.
Here is a little demo.
I have the PHP code below:
<?php
$length = $_GET["length"];
$maxValue = $_GET["maxValue"];
$distribution = array();
for($j = 0; $j < $maxValue; $j++) {
$distribution[j] = 5;
}
$x = 0;
$x++;
for($j = 0; $j < $maxValue; $j++) {
echo $distribution[j] , " ";
}
echo $x;
?>
$x starts as 0 and is incremented by 1. However, just below $x is incremented, I am also incrementing the first element of the "distribution" array - $distribution[0]. And it's not working. It worked fine when I was initializing the elements (set them to 5).
Any ideas on why it might now be working? I am probably referencing the array element wrong. But this seems inconsistent.
When you say $distribution[j] -> php doesn't understand the j as a variable - but rather as an undefined constant
It looks like you are trying to say $distribution[$j] - which is partially -why your increments aren't working - -
The other reason would be that you aren't ever calling $distribution[$j]++ --- so there is no incrementation happening...
I have the next situation, lets say i have an object with 10 attributes, named r1, r2, r3...r10. Now i want to extract the value of each attribute dynamically. for that i make a for like this and know it will work
$sum = 0;
for($i = 1; $i <= 10; $i ++){
$key = "r{$i}";
$sum += $this->$key;
}
This is a representative example, what i want to know is if instead of doing that, i could do something like
for($i = 1; $i <= 10; $i ++){
$sum += $this->r{$i};
}
and take the extra line off... i have tried several forms of concatenate this like that but i cant figure it out. Can any one tell me if it is possible and how.
That's because you don't use +=, use .= when concatenating :-)
Have a read of this: http://www.php.net/manual/en/language.operators.string.php
You can do that :
$sum += $this->{'r'.$i};
Having multiples attributes named like that sounds like a problem for me. Why don't you use an array ?
foreach(($_POST["msg"] as $mg) AND ($_POST["control"] as $id))
{
echo $mg;
echo $id;
}
i need make something like that, any way to do? i'm trying to get 10 mysql records and edit all of them
No, that won't work. The closest thing I can see to what you're trying to do is:
for($i = 0; $i < count($_POST["msg"]); $i++) {
echo $_POST["msg"][$i];
echo $_POST["control"][$i];
}
Assuming that "msg" and "control" will always contain the same amount of items.
Assuming both $_POST['msg'] and $_POST['control'] are actually arrays, have numeric keys (thanks #iMoses), and have the same length, you could use a for loop -
for ($i = 0; $i < count($_POST["msg"]); $i++){
$mg = $_POST['msg'][$i];
$id = $_POST['control'][$i];
}
hello i'm beginner for programming I've got a homework. googled it but couldnt find anything...
i need to get the total value of numbers from 1 to 10. this need to be done in loop. but couldn't figure which loop should i use. if you can also give me an example code thats would be great.
This is a homework question, I'm not sure why people are just giving you an answer to copy-paste.
Achieving the sum of numbers 1..10 is pretty simple. You will need to initialise an empty int var before your loop, and for each iteration from 0 up to and including 10 you will add your int var to the current iteration.
For example:
sum = 0;
for num in range 1 to 10:
sum = sum + num;
<?php
$start = 0; // set the variable that will hold our total
for($i=1;$i<11;$i++){ // set a loop, read here: http://php.net/manual/en/control-structures.for.php for more info
$start += $i; // add $i to our start value
}
echo $start; // display our final value
I would use a for loop.
$total = 0;
for($i = 1; $i <= 10; $i++){
$total += $i;
}
Using the for loop:
<?php
$sum = 0;
for($i = 1; $i <= 10; $i++){
$sum += $i;
}
Using the foreach loop:
<?php
$sum = 0;
foreach(range(1,10) as $num){
$sum += $num;
}
echo $sum; // prints 55
And disregarding your assignment, here is an easier way:
echo array_sum(range(1,10));