-1

I'm having an array of items like this:

$data = array(
            'item1' => array( // is even
                'icon' => 'commenting',
                'content' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. ',
            ), 
            'item2' => array(// is odd
                'icon' => 'sticky-note',
                'content' => 'Debitis id eligendi assumenda, cumque optio veniam eos perferendis molestias explicabo odit',
            ),
            'item3' => array(// is even
                'icon' => 'users',
                'content' => 'Libero, suscipit, quos. Quae praesentium tempore minima quod tempora odio',
            ),
            'item4' => array(// is odd
                'icon' => 'thumbs-o-up',
                'content' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. ',
            ),
            'item5' => array(// is even
                'icon' => 'wrench',
                'content' => 'Debitis id eligendi assumenda, cumque optio veniam eos perferendis molestias explicabo odi',
            ),
        );

What I want to do is, when I loop through the elements of the array in order to output them, to detect whether each element is odd or even, for instance:

foreach ($data as $key => $value) {
    echo '<h1>' . $key . '</h1>';
    echo '<p>' . $value['icon'] . '</p>';
    echo '<p>' . $value['content'] . '</p>';
    echo '<p> (Item is odd or even) </p>'; // * Show wheather is odd or even here
}
ltdev
  • 4,037
  • 20
  • 69
  • 129
  • Do you mean if the number of they key is even/odd ?! e.g. `itemX` if X is even or not ? – Rizier123 Aug 17 '15 at 12:12
  • No the key may not contain numbers at all.. the items themselves whether they are odd/even – ltdev Aug 17 '15 at 12:13
  • I still don't get it. What do you want to check? Do you want to check if the inner array has an even/odd amount of elements? Or if the key is even/odd, when you would assume a 0-based indexed array? – Rizier123 Aug 17 '15 at 12:15
  • If this is used for styling, there's a nice CSS selector that styles just even or odd childs ([see this answer](http://stackoverflow.com/a/5080787/1677209)) – T30 Apr 19 '17 at 11:10
  • [This answer](https://stackoverflow.com/a/24726403/2943403) demonstrates how to maintain the counter and check for oddness. – mickmackusa May 19 '23 at 22:03

5 Answers5

7

Just declare a counter and iterate.

$counter = 1;
foreach ($data as $key => $value) {
    echo '<h1>' . $key . '</h1>';
    echo '<p>' . $value['icon'] . '</p>';
    echo '<p>' . $value['content'] . '</p>';
    echo '<p> ' . (($counter % 2)? 'odd': 'even') . ' </p>'; // * Show whether the position is odd or even here
    $counter++;
}
John Bupit
  • 10,406
  • 8
  • 39
  • 75
3

Using the below code you'd substitute $yourNumber with the variable you want to check. The if statement checks if it is even, and the else will run if it is odd.

<?php
if ($yourNumber % 2 == 0) {
    echo "It is even.";
} else {
    echo "It is odd.";
}
?>

We use the Modulo to check if it's odd/even.

Script47
  • 14,230
  • 4
  • 45
  • 66
3
$i = 1;
foreach ($data as $key => $value) {
    echo '<h1>' . $key . '</h1>';
    echo '<p>' . $value['icon'] . '</p>';
    echo '<p>' . $value['content'] . '</p>';
    echo '<p> ' . (($i % 2)? 'odd': 'even') . ' </p>'; // * Show wheather is odd or even here
    $i++;
}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
1

You can use a counter, the modulus operator and an array to map strings to the result:

$map=['This item is: Even','Whilst this one is: Odd'];
$i=1;
foreach ($data as $key => $value): $i++;?>
    <h1> <?= $key;?> </h1>
    <p> <?= $value['icon'];?> </p>
    <p> <?= $value['content'];?> </p>
    <p> <?= $map[$i % 2];?> </p>
<?php endforeach;?>
Steve
  • 20,703
  • 5
  • 41
  • 67
0

Can go for the below code :

$i = 1;
 foreach ((array) $data as $key => $value) {
    if($i % 2 == 0) $item = 'even';
    else $item = 'odd';
    echo '<h1>' . $key . '</h1>';
    echo '<p>' . $value['icon'] . '</p>';
    echo '<p>' . $value['content'] . '</p>';
    echo '<p> (Item is '.$item.') </p>'; // * Show wheather is odd or even here
    ++$i;
}
Happy Coding
  • 2,517
  • 1
  • 13
  • 24