i have an array:

$input = array("red", "green", "blue", "yellow");

i want to put "<br>" as an element between array elements

output must:

Array
(
[0] => red
[1] => <br>
[2] => green
[3] => <br>
[4] => blue
[5] => <br>
[6] => yellow
)

what do i do?

thanks

    A simple foreach:

    $input = array("red", "green", "blue", "yellow"); 
    $i=1;
    $tmp = array();
    foreach($input as $key=>$val)
    {
        if($i%2 == 0)
            $tmp[] = '<br />';
    
    $tmp[] = $val;
    
    $i++;
    }

    I'm not sure why you'd want to do this though. You can get a string with the <br /> element in between them simple enough using:

    $string = implode('<br />', $input);

    But whatever your fancy 😉 Oh, and the above foreach doesn't worry about the keys of the old array. So associative arrays would be no good going through that.

      Write a Reply...