I have a foreach loop within two other foreach loops. The third one should repeat only once, incrementing the value each time.
It contains the titles for 5 headers in an accordion:
$header_title = array(
'first_link_title',
'second_link_title',
'third_link_title',
'fourth_link_title',
'fifth_link_title',
);
I've tried with
<?php foreach( $header_title as $value): ?>
<?php echo $value;?>
<?php endforeach;?>
But the $value output on each header is
first_link_titlesecond_link_titlethird_link_titlefourth_link_titlefifth_link_title
The expected output should be
Header1 - first_link_title
Header2 - second_link_title
Header3 - third_link title
ecc...
The second loop displays the accordion itself and the first one has nothing to do with the accordion.
Ayy help is much appreciated.
$prefix = 'Header';
$index = 1;
foreach ($header_title as $title) {
echo $prefix . $index . ' - ' . $title . PHP_EOL;
$index ++;
}
I think, this foreach loop must be outside two other foreach loops, not within. In other words those two other foreach loops must be within this "header_title" loop
Related
I know how to count elements inside a loop, but tried and can´t find a solution. Actually, I can count the elements in loop in this way:
$i = 0;
foreach ($Contents as $item)
{
$i++;
}
echo $i;
But in my case, I have large function and I can do this, but need more time for count elements inside. I can´t put the string for showing number elements inside loop outside of the function. I must show inside loop all number of elements and show as this. Here's an example:
$i = 0;
foreach ($Contents as $item)
{
print "Number Elements it´s : $i";
print "Element ".$i."<br>";
$i++;
}
The problem here is that the phrase "number elements it´s" always repeats, because inside loop, and all time $i show me 0,1,2,3,4,5 ...... and all time the same as number of elements inside loop.
My idea and question is if it´s possible inside loop to show the number of elements and after this show the elements as this structure when executing the script:
Númber elements it´s : 12
Element 1
Element 2
Element 3
.
.
.
.......
This it´s my question. I hope you understand it all. Thanks in advance.
As mentioned in the comments, youu will need to use the count() function for that. If you insist on having it inside the loop, simply do it like this:
$i = 0;
foreach ($Contents as $item)
{
if($i == 0) {
print "Number of Elements is : " . count($Contents) . "<br><br>";
}
print "Element ".$i."<br>";
$i++;
}
Can I do this- "foreach($products as $product && $categories as $category)". multiple condition in one foreach loop? If not then how do I proceed this?
Use two loops:
foreach ($products as $product) {
foreach ($categories as $category) {
// using $product and $category
}
}
I had the same question today as the OP. I figured I can split up the problem by first merging the two arrays and then performing the actual foreach loop on the combined array.
This is the idea:
$combined_array = $products + $categories
foreach ($combined_array as $items) {. . .}
Practically speaking, I split the problem into two foreach loops, the first for the merging of the arrays and the second for the actual algorithm.
And indeed it does save me alot of space! Since it is much shorter to append one array to another array than to repeat a complicated foreach algorithm on two separate arrays consecutively.
If your two arrays have the same length, just use a for loop instead and access the elements using the current index.
You just have to make sure, that the indezies are matching in the two arrays.
Its not possible to achieve what you are trying using a foreach loop.
for ($i = 0; $i < count($arr1); $i++) {
// do whatever you want with $arr1[$i] and $arr2[$i]
}
If your array have the same length, but the indizies may differ and you still want them to be on par, use array_values() before the loop to reindex both arrays.
This will only work for index based arrays!
you can't do that.
but what can you do is to loop inside the loop
foreach($categories as $category)
{
foreach($products as $product)
{
}
}
foreach( $codes as $code and $names as $name ) { }
That is not valid.
You probably want something like this...
foreach( $codes as $index => $code ) {
echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}
I have a PHP file, and I'm trying to iterate a RSS feed, I want to assign id's to div tags using the foreach statment for each item in the feed, ie: content0, content1 ...
$rss = simplexml_load_file('--feedURL--');
foreach ($rss->channel->item as $key=>$item) {
echo '<div id="content'.$key.'">" . $item->description . "</div>";
}
I am told this is how you get the index using $key, but all it keeps retuning is the word "item" at the end of the div's id.
You're missing a single-quote in your echo statement:
echo '<div id="content'.$key.'">" '. $item->description . "</div>";
^ ^-- you're missing this single-quote
And, to use sequential IDs in your class name, you can just use counter variable:
$i = 0;
foreach ($rss->channel->item as $key=>$item) {
echo sprintf('<div id="content%d">%s</div>', $i, $item->description);
$i++;
}
Note: I'm using sprintf() instead of concatenating the variables, since this looks a bit more cleaner.
use this
<?php
$rss = simplexml_load_file('--feedURL--');
foreach ($rss->channel->item as $key=>$item) { ?>
<div id='content-<?php echo $key?>'><?php echo $item->description;?></div>
<?php }?>
How do you count the results inside a foreach then store that value in a variable to be used on another foreach.
Example. This foreach returns 5 items
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
}
Now I want that the foreach above be counted and stored in say $totalitems and be used on another foreach. This second foreach also counts its results and store in $totalitems2. Something like this:
foreach ($xml->items as $item) { //Same source but they will be displayed separately based on a condition.
echo "$item->name";
echo "$item->address";
if_blah_blah_meet_condition_then_break;
}
So basically what I want here is to restrict the total number of items being displayed on both foreach combined. $totalitems and $totalitems2 should have the sum of 8. 8 is the number I want limit the items returned. Doesn't matter if the other foreach has more items than the other. 3 and 5. 4 and 4. 6 and 2. Etc.
How can I achieve this? Please advice.
Just use the simple iterator ++ methods. When you are on the second foreach, watch for when $i passes the number that you want to stop it.
Code:
$i = 0;
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
$i++;
}
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
$i++;
if ($i > 5) { // or whatever the number is
break;
}
}
$totalItems = count($xml->items);
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
}
Just use count($xml->items) and that value in the condition, inside the loop.
It seems your array is stored in xml->items therefor, you only would have to save it in another variable if you want to store it.
foreach ($xml->items as $cont=>$item) {
echo "$item->name";
echo "$item->address";
if($cont<8){
$totalItems_one[] = $item;
}
}
//whatever changes you do to $xml->items
foreach ($xml->items as $cont=>$item) {
echo "$item->name";
echo "$item->address";
if($cont<8){
$totalItems_two[] = $item;
}
}
Like this you have the two new arrays totalItems_one and totalItems_two and you can get the number of items just doing count($totalItems_one) or count($totalItems_two) whenever you want.
I am working with SimplePie and I cannot figure out how to output the count, or the key values for the loop.
Shouldn't this
<?php foreach ($feed->get_items() as $item): ?>
<?php
$i = key($item);
echo $i;
?>
<?php endforeach; ?>
, or this
<?php foreach ($feed->get_items() as $item): ?>
<?php
$i = count($item);
echo $i;
?>
<?php endforeach; ?>
be outputting a unique number for each?
uniqid() Doesn't work in this case, because I am running the loop twice on the page and trying to match up one list of elements with another based on ID.
A single argument used with as is interpreted as a variable in which to store the value, not the key. If you want the key, you need to use => in a manner like the following:
foreach ($feed->get_items() as $key => $item):
echo $key;
endforeach
As a sidenote, you're using key() and count() on an item in the array, rather than the array as a whole, which is invalid. As far as I'm aware, there's no guarantee that key() will work as you expect even if applied to the whole array. It's meant for loops where you control the iteration, as with next.
To get the 'count' in a foreach you would need an extra variable. Getting the key is easy and the same if the array is indexed in order instead of associative. Here is an example including both:
$array = array(
'foo' => 'bar'
);
$i = 0;
foreach ($array as $key => $value)
{
/*
code where $i is the 'count' (index) and $key is the associative $key.
*/
/* $i == 0 */
/* $key == 'foo' */
/* $value == 'bar' */
$i++;
}
key($item) that you're using above doesn't work because you're trying to get the key of a value that no longer is associated with the original array.
count($item) is the count of a subarray $item.
you can use get_id() method
like :
foreach ($feed->get_items() as $item)
{
$prev_ids = array('guid1', 'guid2', 'guid3', 'guid4');
if (in_array($item->get_id(true), $prev_ids))
{
echo "This item is already stored.";
}
else
{
echo "This is a new item!";
}
}