1

I have an array with a few values and want to do something like this:

 $arrayvalues = array_reverse(explode(', ', somefunction()));
 foreach ( $arrayvalues as $arrayvalue ) :
           printf('<li class="'.$countvalue.'">'.$arrayvalue.'</li>');  
 endforeach;

I want to have in $countvalue the number of the value in the array

ie... the array will be something like this: ("apple", "orange", "grapefruit")

I want the number to match the order number of these values apple = 1, orange = 2, grapefruit = 3

or actually even if it's just an incremental number according to the values echoed it doesn't matter, I just need to insert a css class represented by an incremembtal number

I tried playing $i... count... but I don't know how to achieve what I want; I'm more a designer than a coder, I looked in the PHP help but couldn't find a clear solution for my case

thank you

4 Answers 4

4

You already have an incremental number based on order. Keep in mind, this only works if your key's are 0-based. If you use an associative array you will need to use a for loop instead (as suggested by nickb).

$arrayvalues = array_reverse(explode(', ', somefunction()));
foreach ( $arrayvalues as $key => $arrayvalue ){
    echo "<li class='$key'>$arrayvalue</li>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what you should do. All the other answers so far are unnecessarily complicated, and complex code leads to buggy code. @LeviMorrison no he doesn't: "actually even if it's just an incremental number according to the values echoed it doesn't matter". The $key value is guaranteed to be unique.
@AbhiBeckert Noted. Also, printf is useless here. I'd suggest echo "<li class='$key'>$arrayvalue</li>"; There's no point not to.
1
 $arrayvalues = array_reverse(explode(', ', somefunction()));
 $i = 0;
 foreach ( $arrayvalues as $arrayvalue )
 {
           $i++;
           printf('<li class="'.$i.'">'.$arrayvalue.'</li>');
 }

10 Comments

regarding to Fulvio question your $i++; needs to be before printf('<li class="'.$i.'">'.$arrayvalue.'</li>'); ("apple", "orange", "grapefruit") I want the number to match the order number of these values apple = 1, orange = 2, grapefruit = 3
-1: I'm not going to advocate mixing foreach and a manual counter when his array is not associate.
@LeviMorrison - It's clean, works, and isn't a performance detriment. Sure, you can use a for loop, but you're just replacing $arrayvalue with $arrayvalues[$i]. I think this method is more intuitive.
@LeviMorrison - Relax. There's no need to get so stressed out about (what you consider to be) a slightly imperfect answer.
@narcisradu - I just copypasted the OP's code and modified it as little as possible so he'd understand it easily.
|
0

Use a for loop to iterate over your array, like so:

 for( $i = 0, $j = count( $arrayvalues); $i < $j; $i++) :
           printf('<li class="' . ($i + 1) . '">' . $arrayvalues[$i] . '</li>');
 endfor;

If you want the index $i to start at one, you need to add one in the printf statement.

Side note: You don't need printf here if you're not actually generating formatted output.

3 Comments

Syntax error: endforeach => endfor I should -1 you for not testing the code you supply as an answer.
+1 for initializing $j and not counting the array in every step of the loop as most of coders those days
@LeviMorrison A -1 for that?! That's not nice... ;)
0
 $arrayvalues = array_reverse(explode(', ', somefunction()));
 $i=0;
 foreach ( $arrayvalues as $arrayvalue ) :
           ++$i;
           $countvalue = $i;
           printf('<li class="'.$countvalue.'">'.$arrayvalue.'</li>');  
 endforeach;

We (or I) advise you use a normal for loop.

for($i = 0; $i < count($arrayvalues); $i++) {
     printf('<li class="'.($i+1).'">'.$arrayvalue.'</li>');  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.