如何将一个值数组转换为另一个索引数组?
这是我被指示分开的另一个问题的后续。我有不同的列集及其值,有些有值,有些没有。例如:
$vCountries ['country1'] = "Value of country1";
$vCountries ['country2'] = "Value of country2";
$vCountries ['country3'] = "";
$vCountries ['country4'] = "Value of country4";
$vCountries ['country5'] = "";
...through to
$vCountries ['country50'] = "Value of country50";
我想生成以下代码示例(仅当它包含值时):
<th id="country1">Value of country1</th>
<th id="country2">Value of country2</th>
<th id="country4">Value of country4</th>
...
由于所有国家/地区 ID 通常都通过索引来命名以区分它们,因此我希望动态生成这些标题。
有人建议使用以下代码:
for ($i = 1; $i <= 50; ++$i) {
$vCountries['country' . $i] = "Value of country " . $i;}
但首先,我不明白它是如何工作的以及如何将其解析为顶部的代码,因为现在它只打印数组数组数组等和“国家的值。$” i" 不是通用的和索引的,它是每个国家/地区不同的实际值,所以我仍然需要手动列出它,不是吗?
有什么想法吗?
谢谢
this is a follow up to another question which I was instructed to split up. I have different set of columns and their values some which have values, some which don't. For example:
$vCountries ['country1'] = "Value of country1";
$vCountries ['country2'] = "Value of country2";
$vCountries ['country3'] = "";
$vCountries ['country4'] = "Value of country4";
$vCountries ['country5'] = "";
...through to
$vCountries ['country50'] = "Value of country50";
I would like to generate the following code example (only when it contains a value):
<th id="country1">Value of country1</th>
<th id="country2">Value of country2</th>
<th id="country4">Value of country4</th>
...
Since all country ID's are generically named with only an index to differ them, I would like these headings to be generated dynamically.
Someone has suggested the following code:
for ($i = 1; $i <= 50; ++$i) {
$vCountries['country' . $i] = "Value of country " . $i;}
But firstly, I don't understand how it is working and how I can parse it to the code at the top because for now it only prints array array array etc. and the "Value of country .$i" is not generic and indexed, it is an actual value different for each country, so I would still have to list it manually, no?
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样就可以了。
这仅输出具有值的条目。每个 id 都会输出为
id,并且每个值都会显示在
标记内。
This will do it.
This outputs only entries with values. Each id is output as the
<th>
id, and each value is displayed inside the<th>
tag.这会生成您想要的代码:
This generates the code you want :
如果您的目标是从已经定义的数组生成上述 HTML 代码,那么这可能是您的解决方案:
If your goal is to generate the said HTML code from the array, which is already defined, then this may be a solution for you: