treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] help with turning a string into array

  • 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);
    ?>
    <ul>
    <?php
    foreach( $item as $items){
    echo '<li>'.$items."</li>";
    }
    wp_reset_query();
    ?>
    </ul>
  • Try this:
    $string = 'Pizza, Piatti al forno, Insalata, Toppings, Pagnottelle';
    $arr = explode(', ', $str);

    for($i = 0; $i < count($arr); $i++) {
    echo '<li> ' .$arr[$i] . "</li>";
    };
  • 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);
    ?>
    <ul>
    <?php
    foreach($items as $menu) {
    echo '<li>'.$menu."</li>";
    }
    wp_reset_query();