如何将一个值数组转换为另一个索引数组?

发布于 2024-10-15 21:14:21 字数 892 浏览 1 评论 0原文

这是我被指示分开的另一个问题的后续。我有不同的列集及其值,有些有值,有些没有。例如:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中性美 2024-10-22 21:14:21

这样就可以了。

foreach($vCountries as $id => $value)
{
    if($value)
    {
        echo "<th id=\"$id\">$value</th>";
    }
}

这仅输出具有值的条目。每个 id 都会输出为 id,并且每个值都会显示在 标记内。

This will do it.

foreach($vCountries as $id => $value)
{
    if($value)
    {
        echo "<th id=\"$id\">$value</th>";
    }
}

This outputs only entries with values. Each id is output as the <th> id, and each value is displayed inside the <th> tag.

静谧 2024-10-22 21:14:21

这会生成您想要的代码:

foreach ($vCountries as $key => $value)
{
    if (strlen($value) > 0)
        echo "<th id=\"$key\">$value</th>";
}

This generates the code you want :

foreach ($vCountries as $key => $value)
{
    if (strlen($value) > 0)
        echo "<th id=\"$key\">$value</th>";
}
灵芸 2024-10-22 21:14:21

如果您的目标是从已经定义的数组生成上述 HTML 代码,那么这可能是您的解决方案:

// Loop the array
foreach ($vCountries as $key => $value) {
    // If value is not empty
    if (!empty($value)) {
        // display the line
        echo '<th id="'.$key.'">'.$value.'</th>'."\n";
    }
}

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:

// Loop the array
foreach ($vCountries as $key => $value) {
    // If value is not empty
    if (!empty($value)) {
        // display the line
        echo '<th id="'.$key.'">'.$value.'</th>'."\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文