1

I have a PHP array such as

$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes')

I am outputting the array as follows via simple foreach

foreach ($lulz as $key) {
  echo $key;
}

I would like to put different CSS classes per each output. so, for example, the output would be

$result = '<div class="one">$tom</div><div class="two">$hojn</div><div class="three">$john</div><div class="four">$kiwi</div><div class="five">$tron</div><div class="six">$jikes</div>';

the current path I am on is putting in an incrementer variable that is incremented ++ after each loop. that works - I get 1, 2, 3, etc - but I am not sure how to translate that to my CSS divs per each item; or if I am even on the right path.

i was hoping to output array such as:

foreach ($lulz as $key) {
  echo $key[1];
  echo $key[3];
}

where [1] and [3] are just the entry 1 and 3 in the array

but doesn't seem to be possible.

any help would be greatly appreciated.

3 Answers 3

1

You can make your array as a class for each output.

Try this

$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');

foreach ($lulz as $key){
  echo '<div class="'.$key.'">'.$key.'</div>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is perfect thank you. using the $key as the class name is genius.
1

If you can just use the incrementer in the class name and it doesn't have to be a word representation of the number, this will give you the desired output and your classes would be .el1, .el2, etc.

<?php
$result = '';
$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');
foreach ($lulz as $key => $value) {
  $result .= '<div class="el' . ($key + 1) . '">' . $value . '</div>';
}   
echo $result;
?>  

5 Comments

$count doesn't seem necessary in a foreach loop - you could easily use $key for the same purpose (all array items are indexed sequentially in this instance, so it's not going to break anything). It would also make more sense to have $value inside the div tag instead of $key. I have upvoted your answer, because you seem to have grasped what the original poster wanted far better than I did. Thank you.
@Pyromonk thanks for the feedback. I rarely use PHP. Updated - is that what you mean?
Perfect! It might be a good idea to initialise $result ($result = '';) before the loop, just to make the code a tad bit cleaner.
@Pyromonk thanks! learning new things today :) Just read about why that's a good idea. php.net/manual/en/language.variables.basics.php "It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used"
ok wow this works great. i am using a multi-dimensional array, is that why the counter is not working for me do you think? i pasted your code and the counter works great, yet when i move to my code it just says el1 over and over
0
<?php
$lulz = array('tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');
$catz = array('one', 'two', 'three', 'five', 'eight', 'thirteen');

foreach($lulz as $k => $x) { echo '<div class="'.$catz[$k].'">'.$x.'</div>'; }
?>

or

<?php
$lulz = array(
    array('tom', 'one'),
    array('john', 'two'),
    array('hojn', 'three'),
    array('kiwi', 'five'),
    array('tron', 'eight'),
    array('jikes', 'thirteen')
);

foreach($lulz as $k => $x) { echo '<div class="'.$x[0].'">'.$x[1].'</div>'; }
?>

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.