PHP, Echo only one time in a foreach loop - php

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

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

How can I get the child key of a key in a multidimensional array?

array(
'World'=>array(
'Asia'=>array(
'Japan'=>array(
'City'=>'Tokyo'
)
)
)
);
In my array I am searching for a key:
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
}
}
}
The result is Japan.
For each key I want to check if the child key is "City". So I did the following:
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
foreach ($key as $k => $i) {
if (stripos($k, "City") !== false){
echo "true";
} else {
echo "false";
}
}
}
}
I expect the result Japan true or at least the result Japan false but the result is still only Japan I do not understand.
In your second foreach your using a single element ($key) while you should use a set of elements ($array[$key], considering it's a multidimensional array).
foreach ($array as $key => $item) {
if(is_array($item)){
if (stripos($key, "Japan") !== false){
echo $key;
foreach ($array[$key] as $k => $i) {
if (stripos($k, "City") !== false){
echo "true";
} else {
echo "false";
}
}
}
}
I'd go with a recursion algorithm to solve the problem:
function find_array_children_key($array, $children_key, $parent_key=''){
$returning_value = false;
if(is_array($array))
{
foreach($array as $key=>$value)
{
if($key===$children_key)
$returning_value = $parent_key;
else
$returning_value = find_array_children_key($array,$children_key,$key);
if($returning_value!==false)
break;
}
}
return $returning_value;
}
Which you'd call, for instance in your case, find_array_children_key($array,'City')

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

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

how to use variable outside foreach loop

How to return $value after loop with its returned data ? I think to create array before loop and equal it to $v to use it after loop but it didn't work.
Any idea on how to solve this problem ?
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
return $v = $value ;
}
echo $v->country_name
try this:
$v = array();
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
{
if(!in_array($value,$v))
{
array_push($v,$value);
}
}
}
try this
$v = array();
$i=0;
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
$i++;
$v[$i] = $value ;
}
//print $v
print_r($v)
If like using 'return' try this.
$v = iLikeUsingReturn($this,$data);
function iLikeUsingReturn($t,$d){
foreach ($t->json_data->locations as $key => $value) {
if ($value->country_name == $d['city']->country_name)
return $value ;
}
return array();
}
I think the following code will helps you.
// create array
$v = array();
// start loop
foreach ($this->json_data->locations as $key => $value) {
if ($value->country_name == $data['city']->country_name)
// return $value with data
array_push($v, $value);
}
return $v;

Categories