PHP Loop through an array second time (foreach) - php

I have a foreach to loop through the data provided by a PDO SQL query:
foreach ($team as $row){
$count++;
$teamNumber = 'team'.$count;
if ($currentScores[0]['team'.$count] == ""){
$red = "red";
}
echo "<strong><font color='".$red."'>".$row['name']."</font></strong>";
echo $currentScores[0]['team'.$count];
if ($count < 2)
echo " vs ";
}
Now, I want to loop again through the $team array but it just returns the last value of the array during the second loop.
I tried the same thing:
foreach ($team as $row) {
.....
.......
}
How could I run again through the array?

Simple enough, just do a foreach loop on $row as you have previously.
foreach($array as $key => $val) {
foreach($val as $a => $b) {
echo $b;
}
}

Related

Storing multiple foreach loops into a array

foreach (array_chunk($html->find('div[class=yemek]'), 4, true) as $array) {
echo '///';
foreach($array as $ul) {
foreach($ul->find('img') as $li) {
echo $li->alt . ',';
}
}
}
I want to store the last result into an array. I have no idea since there is 2 foreach loop. Is it possible? Or I have something in my mind.
assign to the same variable each step, at last you will just the last value.
$last = '';
foreach (array_chunk($html->find('div[class=yemek]'), 4, true) as $array) {
echo '///';
foreach($array as $ul) {
foreach($ul->find('img') as $li) {
echo $li->alt . ',';
}
}
}
$lastArray = [$last];

Loop into an array with PHP

I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.

PHP, Echo only one time in a foreach loop

i'have an array, and then i have a script who gets the category i'm browsing (using wordpress) and put it in the $category variable.
So i test if the category i'm browsing it's equal to the $array key and then i paste some text
$array = array ('key' => 'value', ... )
//...
// a script who gets the category i'm browsing and store it in the $category variable
//...
/* starting the foreach loop */
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
echo "nothing";
}
The problem is that this loop does echo "nothing" for each time the $category is not equal to the $key for each element of the array.
So if i have 20 key => value in the array this loop paste one time "some $value here" and 19 times "nothing"
there is a way to echo "nothing" only one time?
Thank you!
You can use array_key_exists instead of the foreach loop:
if (array_key_exists($category, $array)) {
echo $array[$category];
} else {
echo 'nothing';
}
$i=0;
foreach( $array as $key => $value) {
$i++;
if ($category == $key) {
echo "some $value here";
} elseif($category !== $key)
{
if($i<=1)
{
echo "nothing";
}
else{}
}
Try with -
$i = 1;
foreach( $array as $key => $value) {
if ($category == $key) {
echo "some $value here";
} elseif ($category !== $key) {
$i++;
}
}
if (count($array) == $i) {
echo "nothing";
}

Using an if statement to alter a foreach loop

I've got a loop. It echoes each item in an array. However, I want to wrap one of the items with some custom content. At the moment, I can do that, but it repeats it unnecessarily. Here is my loop:
$arr = array(1,2,3,4,5,6);
foreach ($arr as $key) {
if ($key == 5) {
echo 'wrap';
echo $key;
echo 'wrap';
}
echo $key;
}
Which produces:
1
2
3
4
WRAP
5
WRAP
5 <--- remove
6
As you can see, the $key 5 is being duplicated. I just need to wrap it once when it's called. Is there a way to only echo 5 once?
As it's currently written, the echo statement after your if block will get executed on each loop iteration. You only want that to happen when the value of $key is not 5. So use the else block:
foreach ($arr as $key) {
if ($key == 5) {
echo 'wrap';
echo $key;
echo 'wrap';
} else {
echo $key;
}
}
What about this:
$arr = array(1,2,3,4,5,6);
foreach ($arr as $key) {
if ($key == 5) {
echo 'wrap';
echo $key;
echo 'wrap';
} else {
echo $key;
}
}
Yep, simple, just add an 'else' statement:
$arr = array(1,2,3,4,5,6);
foreach ($arr as $key) {
if ($key == 5) {
echo 'wrap';
echo $key;
echo 'wrap';
} else {
echo $key;
}
}

Process array starting with second entry?

I have an array in PHP and would like to use foreach to process entries skipping [0], to process [1], [2], etc.
Thank you
you can use array_slice
$array = array(1,2,3);
foreach (array_slice($array,1) as $value ) {
echo $value;
}
If you don't mind losing first element you can use array_shift
array_shift($array);
foreach ( $array as $value ) {
echo $value;
}
Output
23
$i = 0;
foreach ($ar as $value) {
if ($i > 0) {
// code here
}
$i++;
}
You can keep a variable for this:
$firstSkipped = false;
foreach ($arr as $value) {
if (!$firstSkipped) {
$firstSkipped = true;
continue;
}
// code here
}
Or you could just use a regular for loop, setting the beginning counter to 1:
for ($i = 1, $count = count($arr); $i < $count; $i++) {
// code here
}
You can remove the first entry from an array with array_shift.
$array = array("a","b","c");
array_shift($array);
foreach ($array as $values)
{
echo $values; //bc
}
Try this:
$arr = array(0,1,2,3,4,5);
unset($arr[0]);
foreach($arr as $value) {
echo $value;
echo "<br />";
}
This would delete first entry from array, so it would not skip as you asked, but anyway you can try this...

Categories