0

My current array

[attributes] => Array
    (
        [0] => Color, Black, 1
        [1] => Size, S, 0
    )

On foreach how to display this array to be something like this

<ul>
  <li class="alignleft">Color : Black</li>
  <li class="alignright">$ 1.00</li>
  <li class="alignleft">Size : S</li>
  <li class="alignright">$ 0.00</li>
</ul>

What I've done..

<ul>            
<?php
$c = true;
foreach ($_SESSION['checkout']['attributes'] as $key => $value) {
    echo '<li'.(($c=!$c)?' class="alignright"':' class="alignleft"').">$value</div>";
}
?>
</ul>

1
  • I think you need to rethink your array structure - something more like array('Color'=>'black', 'price'=>1)... Commented Feb 22, 2012 at 0:48

3 Answers 3

4

As long as all your array entries look like that, this should work:

<ul>            
<?php
foreach ($_SESSION['checkout']['attributes'] as $key => $value) {
    $arr = explode(",", $value);
    echo '<li class="alignleft">' . $arr[0] . ' : ' . $arr[1] . '</li>';
    echo '<li class="alignright">$' . number_format($arr[2], 2) . '</li>';
}
?>
</ul>
Sign up to request clarification or add additional context in comments.

2 Comments

This would work. Also, your answer from my post: Arrays from multiple upload form, Upload images then insert to database (PHP, MySQL) I would have marked correct if you didn't delete it. (Your comment)
@Love Ha, well I didn't realize when posting the answer that someone posted a somewhat similar one 10 minutes prior. I undeleted since you saw it worthy. Thanks.
3

Personally I prefer to use sprintf if I have to output a short html content. For easier readability of course.

<ul>
    <?php
    foreach ($_SESSION['checkout']['attributes'] as $key => $row) {
        list($label, $type, $price) = explode(", ", $row);
        echo sprintf('
        <li class="alignleft">%s : %s</li>
        <li class="alignright">$ %.2f </li>
        ', $label, $type, $price);
    }
    ?>
</ul>

Comments

2

The solution is:

<ul>            
<?php
foreach ($_SESSION['checkout']['attributes'] as $key => $value) {
    $val = explode(', ', $value);
    echo '<li class="alignleft">'. $val[0]. ' : '. $val[1]. '</li>';
    echo '<li class="alignright">$ '. number_format($val[2], 2). '</li>';
}
?>
</ul>

A better way, however, would be if you could have more control over your array and use something like:

Array(

    [0] => Array(
        ['label'] => 'Color',
        ['value'] => 'Black',
        ['price'] => 1
    ),
    [1] => Array(
        ['label'] => 'Size',
        ['value'] => 'S',
        ['price'] => 0
    )
)

Then you wouldn't need to explode the $value and you could use $value['label'] instead of $val[0] and $value['value'] instead of $val[1], etc...

3 Comments

Nice...almost the same answer. But you'll want to concatenate your strings with periods, not commas.
Ok, thanks. Is it faster to concatenate the strings before passing it as parameter(s) to echo, rather than just using the variable parameters feature in echo? [edit: faster, not fast]
I knew echo was a construct and forgot it could be used with commas since I rarely see it used that way. So your way worked as well. My bad.

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.