使用计数器访问数组中的数据
我有一个像这样的数组结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要使用
for
循环,foreach
就足够了。这样您就不必处理超出边界(未设置索引)警告。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.您收到警告是因为您正在访问不存在的
$data
元素。for
条件中的边界检查太高,count
将返回 2,但$data[2]
未设置,无法设置然后在foreach
中使用。相反,(Demo):
当然,使用外部
foreach
也更容易:这基本上做同样的事情,你不需要关心计数器变量。
接下来,您需要决定要显示哪些相关数据。您可以选择性地选择它们,而不是显示所有项目:
然后您可能想要标记输出:
You get the warning because you are accessing an element of
$data
that does not exists. The boundary check in thefor
condition is one too high,count
will return 2, but$data[2]
is not set and can not be used withinforeach
then.Instead do (Demo):
Naturally it's easier to use an outer
foreach
as well: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:
Then you might want to label the output: