How to change the class of the first echo result in PHP?

Easy, but I can not find the answer.

I want to list the subcategories for creating tabs in the code below. This works well on my way out. But the first tab requires <li class= "selected" > to display the content on the page load.

How to get the first echo result to include the class in the <li> element?

.

.

.

[Output]

Actual: .......... Category 1 | Category 2 | Category 3

Requires: .......... Category 1 * | Category 2 | Category 3

* class = "selected"

  <ul class="cd-tabs-navigation"> <?php $argumentos = array( 'hide_empty' => 1, 'child_of' => 44, ); $categories = get_categories($argumentos); foreach($categories as $category) { echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; } ?> </ul> 

.

.

[EDIT]

Solution by Lal. Thanks everyone!

  foreach($categories as $category) { if(++$i==1) echo '<li class="selected"><a class="selected" data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; else echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; } 
+5
source share
3 answers

I would suggest you use a counter. Not sure if this is the right way to do this.

Change foreach as follows

 $i=0; foreach($categories as $category) { if($i==0) echo '<li class="selected"><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; else echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; $i++; } 

OR

As shown in the answer here , you can use array_shift() to process the first element in a separatley array .

That is, do it as shown below

 $first = array_shift($categories); echo '<li class="selected"><a data-content="' . $first ->slug . '" href="#' . $first ->slug . '">' . $first ->name . '</a></li>'; foreach($categories as $category) { echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>'; } 

More on array_shift() in docs

+3
source

The Lal solution will work fine (as long as you increase $i ), but here's a simpler way to do this without an extra variable for the counter:

 foreach($categories as $index => $category){ if(!$index) echo "This is the first element"; else echo "This is not the first element"; } 

(not sure if this should be an edit or an answer, since it is basically the same method, but with slightly simpler synthax, I will send as an answer for now)

+1
source

You can use this syntax for foreach. Using this syntax, you have the iteration index in $ k and the value of the array you are repeating in $ v. So, if $ k == 0, you are at the first iteration.

 $a = array("first", "second", "third", "fourth"); foreach ($a as $k=>$v) { echo $k . "--->" . $v ; if($k == 0){ echo "First iteration!"; //Do your stuff } echo "<br>"; } 
-1
source

Source: https://habr.com/ru/post/1232742/


All Articles