I'm noob in PHP programming and for my surprise I found it difficult to create an array using loop and to input in it values using arithmetic progression with +4 difference.I spent over an hour and tried a lot of code,searched so many examples.Below is my code that work(maybe) but not properly.
<?php
$array = [];
for($x=0;$x<10;$x++){
for($i=0;$i<100;$i+=4){
$array[] = $i;
}
break;
}
var_dump($array);
?>
I must have no more than 10(0-9 key) values,but because of $i the loop continues to 96 up to 24 keys.Maybe it's stupid question but I've totally blocked.
Is that what you want ?
<?php
$array = [];
for($x=0;$x<10;$x++){
$array[] = $x*4;
}
var_dump($array);
?>
Or maybe simpler
$array = range(0,36,4);
Doc for range : http://php.net/manual/fr/function.range.php
Then perhaps you have been overthinking this. You just need one loop, and can simply scale your key by 4:
foreach (range(0, 10) as $x) {
$array[] = 4 * $x;
}
Which will just add 0 for key 0, and 4 for key 1, and so on.
Note that for larger ranges, you should keep the classic for of course. It's more readable/obvious for math thingys anyway.
Use this:-
for ($x = 0; $x < 10; $x++) {
$array[$x] = $x * 4;
}
echo '<pre>';
print_r($array);
I think you must read basic of array here is a link that is useful for you link
I am trying to learn php, and I am playing around with while loops. I was wondering how to print out a specific number in an array in php. Fx:
$a = [1,3,5,7,9,11,13];
$s = 3;
while($a == 3) {
echo $s.' is in the row';
$a++;
}
In this example I would like to run through the $a and see if 3 exist there. If it does it has to echo '3 is in the row' I tried to make a while loop, but it is not correct. Can anyone see what I am doing wrong? Just to say it, I think it is very wrong, but I don't know how to solve it, if I have to use the while loop?
Best Regards
Mads
Your while condition reads: "While the value of $a equals 3", but $a is an array, so its value can't ever be 3. The loop will never be executed. In PHP, we would write:
if (in_array($s, $a))
echo $s, ' was found in the array';
Or, if you insist on writing loops:
foreach ($a as $key => $value)
{
if ($value == $s)
{
echo $s, ' was found at offset ', $key;
break;//end terminate loop
}
}
Of course, you could also write:
for ($i=0, $j=count($a);$i<$j;++$j)
{
if ($a[$i] == $s)
{//you could move this condition to the loop itself, even
echo $s, ' found in array at offset ', $i;
break;
}
}
You can, if you want use a while loop, too, but that wouldn't be the best choice for your particular case. Just read through the manual on php.net. There are many, many array_* functions available, and there are many ways to iterate over your data.
Another worry is your using the array name as a sort-of C-style pointer: $a++; in C, an pointer can be incremented to set it to point to the next value in an array (if the new memory address is valid, and the pointer is valid, and all of the other things you have to worry about in C). PHP does not work this way. An array isn't really an array: it's a hash map. incrementing an array, therefore, is pointless and most likely to be a bug. The for loop is the closest you can get to traversing an array using the ++ operator.
You're looking for in_array. This checks if a value exists in an array, in the form of:
in_array ( mixed $needle , array $haystack )
So, in your case, you'd want to do:
$a = [1,3,5,7,9,11,13];
$s = 3;
if (in_array($s, $a)) {
echo $s.' is in the row';
}
foreach($a as $b) {
if($b == 3)
echo $b.' is in the row';
}
Modify slightly your code changing while condition:
$a = array(1,3,5,7,9,11,13);
$s = 3;
$counter = 0;
while($counter < count($a)) {
if ( $a[$counter] == $s )
echo $s.' is in the row';
$counter++;
}
Added counter to iterate through while loop until end of array.
count() method returns number of items in array.
This solution prints all occurences of your number.
To have better code, change names of variables:
$numbers = array(1,3,5,7,9,11,13);
$target = 3;
$counter = 0;
while($counter < count($numbers)) {
if ( $numbers[$counter] == $target )
echo $target.' is in the row';
$counter++;
}
There are two ways to do it,
First, you can loop through all items in the array using a foreach() loop.
That way, you can go through them all and if you have multiple conditions, it makes your code a bit more readable.
And example of that loop is like this:
foreach($array as $array_item) {
if($array_item === 3) {
echo "3 is in the array";
}
}
The alternative is to use a built in function to find if something is in the array. THis is probably much faster, though I haven't benchmarked the difference.
if(in_array(3, $array)) {
echo "3 is in the array";
}
you can use
array_search ,in_array , and forearch or for loops to itertate through the array.
For learning purposes
$a = [1,3,5,7,9,11,13];
$s = 3;
for($i=0;$i<count($a);$i++)
{
if($a[$i]==$s){
echo $s.' is in the row';
}
}
of course in real life
if (in_array(3, $a)) {
// Do something
}
would be better;
<?php
$a = [1,3,5,7,9,11,13];
$s = 3;
for($a=0;$a < 20; $a++)
{
while($a == 3) {
echo $s.' is in the row';
//$a++;
}
}
?>
Title is lengthy and confusing, forgive me.
$array = (1,5,10,25,50);
$x = 8
How would I compare $x to each value within the array, and then select the value with the closest match.
In this case, it would be 10.
I imagined creating a handful of if statements but thought there could be a better way to do this.
Thanks in advance
Another way, using an intermediate array with the differences:
$diff = array();
foreach($array as $n)
$diff[$n] = abs($x - $n); // key = number, value = difference
// get the key that contains the smallest difference
$closest = array_search(min($diff), $diff);
$min = 0;
foreach ($array AS $i => $v) {
if (abs($array[$min] - $x) > abs($v - $x))
$min = $i;
// you can optimize this with :
if ($v == $x)
break;
}
$closest = $array[$min];
Something like that should work.
I'm trying to figure out if its possible to stop a foreach loop in PHP, like this,
$arr = array('Joe', 'Jude', 'James', 'Pitch', 'Tim');
$i=0;
foreach($arr as $val){
echo $val;
if($i == 2){
//Stop looping
}
}
Is there anyway how to do that, if yes how do I that?
Thank you.
Use break and increment $i.
$arr = array('Joe', 'Jude', 'James', 'Pitch', 'Tim');
$i=0;
foreach($arr as $val){
echo $val;
if(++$i == 2){
break;
}
}
You can use good old keyword break here as well. Followed by a semicolon ; of course.
if($i == 2){
break;
}
Manual for break
You can break out of a loop with break.
1)As said earlier u need to increment the loop.
2)...if ur using multiple "foreach" that are nested,you can do break 'n' to get out of n nested loop,where n will specify any number from 1 to the total number of loops that you have used.Hope it helps!
Simple question:
Why is this:
for($k=1;$k<=10;$k+2) { }
giving an infinite loop? When I change $k+2 by $k++, it works fine.
How can I correct it? (I can't change the 10 for an impair number because I need this function to work either with a pair or impair value at that place)
$k+2
This won't change $k's value, so it never get's higher than 10. Probably you meant:
$k+=2
Which will increase $k by two each time the expression get's evaluated (at the end of each for-loop).
for($k=1;$k<=10; $k = $k+2) { }
or
for($k=1;$k<=10; $k += 2) { }
it is infinite loop because $k is not updated, try this instead
for($k = 1; $k <= 10; $k = $k + 2) {}
or
for($k = 1; $k <= 10; $k += 2) {}
Reference: PHP operators