简体   繁体   English

如何从单个变量创建数组并形成多个Foreach循环?

[英]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. 我对HTML / CSS相当精通,但是在涉及PHP和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. 我已经跃跃欲试地编写Wordpress短代码(基本上是php函数),到目前为止,通过反复试验和看似无休止的浏览器刷新,我已经能够弄清楚一切。 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" 基本上,我正在尝试为属性提供值列表,例如: attr="23, 95, 136, ect"

The function then needs to take that attribute variable and create an array with it: $arr = array($attr); 然后,该函数需要使用该属性变量并使用它创建一个数组: $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. 完成此操作后,我想创建一个foreach循环,以解析列表中的每个数字,如果可能的话,可能还要通过另一个foreach循环,并为每个数字返回一段代码,但我不确定如何将其提取出来。

Any feedback would be greatly appreciated. 任何反馈将不胜感激。

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

gives array(23, 95, 136) . 给出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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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