How to Create an Array from a Single Variable and Form Multiple Foreach Loops?

I’m fairly proficient in HTML/CSS, but very new when it comes to the likes of PHP and Javascript. I’ve jumped headfirst into coding WordPress shortcodes (basically php functions), and so far, through trial and error and seemingly endless browser refreshes, I’ve been able to figure everything out. I just hit a huge wall though, hence why I’m here.

Basically, I’m trying to give an attribute a list of values like: attr="23, 95, 136, ect"

Read More

The function then needs to take that attribute variable and create an array with it: $arr = array($attr);

To me that seems as if it would work, but the array takes the whole list as one value instead. After doing that I want to create a foreach loop that parses each number from the list, possibly through yet another foreach loop if possible, and returns a section of code for each one, and I’m not quite sure how to pull that off either.

Any feedback would be greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. explode(", ", "23, 95, 136")
    

    gives array(23, 95, 136). See the manual.

    If you wish, you can then iterate throw the several values:

    $data = "23, 95, 136";
    $arr = explode(", ", $data);
    foreach ($arr as $value) {
        //$value will take the value 23, then 95, and finally 136
    }