How to output comma-separated array?
-
I’ve got this code, and I’ve almost got it working, but I can’t a) get the code to output in the right place, and b) get it to output separately in each list item properly.
I have a meta field with comma-separated data in it, and I want to explode the comma-separated data into individual list items.
Here is the code I have so far:
<div class="thumbnails"><h2>Additional Colors</h2><ul class="additional_colors"><?php $attachments = get_posts( array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'post__not_in' => array( get_post_thumbnail_id() ), 'post_mime_type'=> 'image', 'orderby' => 'menu_order', 'order' => 'ASC' ) ); if ($attachments) { $loop = 0; $columns = apply_filters( 'product_thumbnails_columns', 3 ); $additional_colors = array(get_post_meta($post->ID, 'additional_colors', true)); foreach ( $attachments as $key => $attachment ) { if ( get_post_meta( $attachment->ID, '_exclude_image', true ) == 1 ) continue; $classes = array( 'zoom' ); if ( $loop == 0 || $loop % $columns == 0 ) $classes[] = 'first'; if ( ( $loop + 1 ) % $columns == 0 ) $classes[] = 'last'; if($additional_colors) { foreach ( $additional_colors as $additional_color ) { echo $additional_color; } } printf( '<li><a href="%s" title="%s" rel="thumbnails" class="%s">%s</a> %s</li>', wp_get_attachment_url( $attachment->ID ), esc_attr( $attachment->post_title ), implode(' ', $classes), wp_get_attachment_image( $attachment->ID, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ) ), explode(',', $additional_color ) ); $loop++; } } ?> </ul> </div>
It’s the additional colors that I’m trying to get to work right. It outputs the code like this (summarized):
<div class="thumbnails"><h2>Additional Colors</h2> <ul class="additional_colors"> data,data,data,data <li><a><img></a> <a>Array</a></li> data1,data2,data3,data4 <li><a><img></a> <a>Array</a></li> data1,data2,data3,data4 <li><a><img></a> <a>Array</a></li> data1,data2,data3,data4 <li><a><img></a> <a>Array</a></li> </ul> </div>
When I want it to be (summarized):
<div class="thumbnails"><h2>Additional Colors</h2> <ul class="additional_colors"> <li><a><img></a> <a>data1</a></li> <li><a><img></a> <a>data2</a></li> <li><a><img></a> <a>data3</a></li> <li><a><img></a> <a>data4</a></li> </ul> </div>
Basically it outputs all of the comma-separated stuff as one line, outside of the list item, and in the place of the list item, it just outputs “Array”. I’m novice at PHP, just hacking away other code trying to learn. Any help?
- The topic ‘How to output comma-separated array?’ is closed to new replies.