简体   繁体   中英

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"

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM