Using an if statement to alter a foreach loop - php

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;
}
}

Related

iteration based printing in php for each loop

Hi i am trying to print only 'a' on first iteration of for each loop and only 'b' on second iteration and only 'c' on third iteration.
<?php foreach ($results['questions'] as $result) {
echo "string";
}
You don't have clarified the $result[] array, nor you have specified which thing to compare for having a or b or c. Anyhow it may be like:
foreach($results['questions'] as $result)
{
if($result['question_no'] == 'a')
{
echo 'a'; // or something else
}
else if($result['question_no'] == 'b')
{
echo 'b'; // or something else...
}
...
...
}
i don't have idea about your array so, i'm use this simple array. try this code, use counter and check your iteration.
DEMO
<?php
$i = 0;
$data = ['a','b','c'];
foreach($data as $val)
{
$i++;
if($i == 1)
{
echo $val; // print a
}
if($i == 2)
{
echo$val; // print b
}
if($i == 3)
{
echo $val; // print c
}
echo "\n";
}
What is the structure of $results ?
Let's say it is as follows:
$results = ['a' => 'result of A',
'b' => 'result of B',
'c' => 'result of C'];
Use function array_keys to get keys from your array $results:
$resultKeys = array_keys($results);
Then you just echo each key with foreach function:
foreach($results as $key){
echo $key . PHP_EOL;;
}

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];

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";
}

PHP Loop through an array second time (foreach)

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;
}
}

foreach trying to make 3 parameters

Is there a way I could make this happen? Its like foreach with 3 paramaters
Something like this
foreach ($value as $v,$value2 as $v2,$value3 as $v3)
<?php echo $v->name?>
<?php echo $v->actual?>
<?php echo $v2->estimated?>
<?php echo $v3->projected?>
I think you want
foreach ($value as $v)
{
foreach($value2 as $v2)
{
foreach($value3 as $v3)
{
echo $v->name;
echo $v->actual;
echo $v2->estimated;
echo $v3->projected;
}
}
}
Or:
foreach ($value as $v,$value2 as $v2,$value3 as $v3)
{
echo $v->name;
echo $v->actual;
}
foreach($value2 as $v2)
{
echo $v2->projected;
}
foreach($value3 as $v3)
{
echo $v3->estimated;
}
Which you may have known, but I don't think it's possible to actually put all three into one like you're asking.
EDIT: With a bit more information on what your $value arrays contain, it may be easier to provide you with a solution that can help you more easily accomplish what you're trying to do.
To retrieve next elements from each array in every iteration:
do{
$v = current($value);
$v2 = current($value2);
$v3 = current($value3);
// ...
} while(next($value) !== false && next($value2) !== false && next($value3) !== false);
Assuming they have equal length, are not empty and contain no falses.
You may also want to use for loop:
for($i = 0; $i < count($value); $i++){
$v = $value[$i];
$v2 = $value2[$i];
$v3 = $value3[$i];
// ...
}
Assuming their keys are numeric and they have equal length.

Categories