foreach输出两个数组首先跳过[重复]
Possible Duplicate:
how to skip elements in foreach loop
I have the following foreach:
foreach($documents as $document):
print_r($document);
endforeach;
Which outputs the following:
Array
(
[num] => 2
)
Array
(
[0] => Array
(
[name] => Batman
[url] => http://batman.com
)
[1] => Array
(
[name] => Superman
[url] => http://superman.com
)
)
The first array conatining [num] => 2, I dont want to use in my foreach when printing the result.
But how do I get rid of that array so it doesn't get printed when I use write print_r($document)?
可能重复: strong>
如何跳过foreach循环中的元素 p> blockquote> \ n我有以下foreach: p>
foreach($ documents as $ document): print_r($ document); endforeach; code> pre>
输出以下内容: p>
Array ( [num] => 2 \ n) Array ( [0] =>数组 ( [名称] =>蝙蝠侠 [url] => http://batman.com ) [1] =>数组 ( [名称] =>超人 [url] => http://superman.com ) ) code> pre>
第一个包含[num] =>的数组 2,打印结果时我不想在我的foreach中使用。 p>
但是如何摆脱那个数组,以便在我使用write print_r($ document)时不打印? p> div>
Keeping with the foreach
you can use continue
:
$first = true;
foreach($documents as $document) {
if($first) {
$first = false;
continue;
}
print_r($document);
}
Use a standard for
loop with an incrementing index specifier and skip the first element.
for($i = 1; $i < count($documents); $i++) {
print_r($documents[i]);
}
The easiest approach would be to remove the first array completely, however my guess is that you can't do that. No worries - this should have you covered:
for( $i = 1; $i < count($documents); $i++ ):
print_r($documents[$i]);
endfor;
Edit: I've created a test case for you on Codepad.org.