使用计数器访问数组中的数据

发布于 2024-12-05 12:33:17 字数 1548 浏览 1 评论 0原文

我有一个像这样的数组结构:

Array
(
    [0] => Array
        (
            [term_id] => 22
            [name] => Aaa Aa
            [slug] => aaa-aa
            [term_group] => 0
            [term_taxonomy_id] => 22
            [taxonomy] => category
            [description] => 
            [parent] => 13
            [count] => 0
            [cat_ID] => 22
            [category_count] => 0
            [category_description] => 
            [cat_name] => Aaa Aa
            [category_nicename] => aaa-aa
            [category_parent] => 13
        )

    [1] => Array
        (
            [term_id] => 11
            [name] => adasdasda
            [slug] => adasdasda
            [term_group] => 0
            [term_taxonomy_id] => 11
            [taxonomy] => category
            [description] => asdsdad
            [parent] => 1
            [count] => 0
            [cat_ID] => 11
            [category_count] => 0
            [category_description] => asdsdad
            [cat_name] => adasdasda
            [category_nicename] => adasdasda
            [category_parent] => 1
        )
)

我想知道如何轻松循环数据以进行输出...

例如,我想显示所有名称、描述和数据块。

我不知道如何显示它们全部...

这就是我所拥有的,并且它有点有效,但是它抛出了无效的参数。

    for ($i = 0; $i <= count($data); $i++) {
    foreach($data[$i] as $key => $value){
            echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
        }
    }

I have an array structure like so:

Array
(
    [0] => Array
        (
            [term_id] => 22
            [name] => Aaa Aa
            [slug] => aaa-aa
            [term_group] => 0
            [term_taxonomy_id] => 22
            [taxonomy] => category
            [description] => 
            [parent] => 13
            [count] => 0
            [cat_ID] => 22
            [category_count] => 0
            [category_description] => 
            [cat_name] => Aaa Aa
            [category_nicename] => aaa-aa
            [category_parent] => 13
        )

    [1] => Array
        (
            [term_id] => 11
            [name] => adasdasda
            [slug] => adasdasda
            [term_group] => 0
            [term_taxonomy_id] => 11
            [taxonomy] => category
            [description] => asdsdad
            [parent] => 1
            [count] => 0
            [cat_ID] => 11
            [category_count] => 0
            [category_description] => asdsdad
            [cat_name] => adasdasda
            [category_nicename] => adasdasda
            [category_parent] => 1
        )
)

I was wondering how to go about easily looping through the data for output...

For example, I want to display all the names, descriptions and slugs.

I can't figure out how to display them all...

This is what I have, and it kind of works, however it's throwing and invalid argument.

    for ($i = 0; $i <= count($data); $i++) {
    foreach($data[$i] as $key => $value){
            echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

伴随着你 2024-12-12 12:33:17

不需要使用for循环,foreach就足够了。这样您就不必处理超出边界(未设置索引)警告。

$array = array(); // <- your data here
foreach ($array as $arr) {
    echo 'Name: ', $arr['name'], '<br />',
         'Description: ', $arr['description'], '<br />',
         'Slug: ', $arr['slug'], '<br />';
}

No need to use the for loop, foreach will suffice. This way you won't have to deal with the out of boundary (index not set) warning.

$array = array(); // <- your data here
foreach ($array as $arr) {
    echo 'Name: ', $arr['name'], '<br />',
         'Description: ', $arr['description'], '<br />',
         'Slug: ', $arr['slug'], '<br />';
}
怪我入戏太深 2024-12-12 12:33:17

您收到警告是因为您正在访问不存在的 $data 元素。 for 条件中的边界检查太高,count 将返回 2,但 $data[2] 未设置,无法设置然后在 foreach 中使用。

相反,(Demo):

for ($i = 0; $i < count($data); $i++) { # $i < count, not $i <= count
    foreach($data[$i] as $key => $value){
        echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
    }
}

当然,使用外部 foreach 也更容易:

foreach($data as $oneData)
{
    foreach($oneData as $key => $value)
    {
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

这基本上做同样的事情,你不需要关心计数器变量。

接下来,您需要决定要显示哪些相关数据。您可以选择性地选择它们,而不是显示所有项目:

$displayKeys = array('name', 'description', 'slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key)
    {
        $value = $oneData[$key];
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

然后您可能想要标记输出:

$displayKeys = array('name' => 'Name', 'description' => 'Description', 'slug' => 'Slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key => $label)
    {
        $value = $oneData[$key];
        echo "$label: $value<br />\n"; 
    }
}

You get the warning because you are accessing an element of $data that does not exists. The boundary check in the for condition is one too high, count will return 2, but $data[2] is not set and can not be used within foreach then.

Instead do (Demo):

for ($i = 0; $i < count($data); $i++) { # $i < count, not $i <= count
    foreach($data[$i] as $key => $value){
        echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
    }
}

Naturally it's easier to use an outer foreach as well:

foreach($data as $oneData)
{
    foreach($oneData as $key => $value)
    {
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

This basically does the same and you don't need to care about the counter variable.

The next thing is that you need to decide which of the data in question you would like to display. Instead of displaying all items, you can pick them selectively:

$displayKeys = array('name', 'description', 'slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key)
    {
        $value = $oneData[$key];
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

Then you might want to label the output:

$displayKeys = array('name' => 'Name', 'description' => 'Description', 'slug' => 'Slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key => $label)
    {
        $value = $oneData[$key];
        echo "$label: $value<br />\n"; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文