Grow your CSS skills. Land your dream job.

help with turning a string into array

  • # January 30, 2012 at 3:28 am

    I am working in wordpress. I am using a custom field to get an set of values. Right now, it returns this into my theme.

    Pizza, Piatti al forno, Insalata, Toppings, Pagnottelle,

    I tried using explode() to echo it out into an unordered list using the comma as the delimeter but all I could get was

    Array

    or

    Array ( [0] => Array )

    So I set it back to get the values above. Here is what I have right now


    < ?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $item = get_post_meta($postid, 'lunch menu', true);
    ?>

      < ?php
      foreach( $item as $items){
      echo '
    • '.$items."
    • ";
      }
      wp_reset_query();
      ?>
    # January 30, 2012 at 3:35 am

    Try this:

    $string = 'Pizza, Piatti al forno, Insalata, Toppings, Pagnottelle';
    $arr = explode(', ', $str);

    for($i = 0; $i < count($arr); $i++) {
    echo '
  • ' .$arr[$i] . "
  • ";
    };
    # January 30, 2012 at 3:49 am

    thanks for the help Jamy_za but I figured it out about 3 seconds after getting your email. I think my foreach loop was wrong. Here is how I did it


    global $wp_query;
    $postid = $wp_query->post->ID;
    $item = get_post_meta($postid, 'lunch menu', true);
    $items = explode(",", $item);
    ?>

      < ?php
      foreach($items as $menu) {
      echo '
    • '.$menu."
    • ";
      }
      wp_reset_query();

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".