在 simpleXMLElement 的 foreach 循環(huán)中獲取子索引
get the sub index in foreach loop in simpleXMLElement(在 simpleXMLElement 的 foreach 循環(huán)中獲取子索引)
本文介紹了在 simpleXMLElement 的 foreach 循環(huán)中獲取子索引的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
這是我的php代碼:
<ul><?php$xml = simplexml_load_file('post.xml');//print_r($xml);foreach($xml->item as $key=>$item ){回聲<<<HTML<li><div class="已發(fā)布"><span class="day">13</span>2010 年 9 月
<div class="summary"><a href="#slide-$key"><h3>$ite??m->title</h3></a>
HTML;}?>
php 渲染頁(yè)面后,
返回
.我想要 $key 獲取其中的數(shù)量,那么如何獲得?
解決方案
這是因?yàn)?simpleXML 結(jié)構(gòu)不是普通的數(shù)組,而是沒(méi)有數(shù)組索引的迭代器.
除了使用變量手動(dòng)跟蹤之外,我不知道還有其他解決方案:
$index = 0;$xml = simplexml_load_file('post.xml');foreach($xml->item 作為 $item ){....$索引++;}
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<title>This is title1</title>
<desc>This is desc1</desc>
<image></image>
<tudou></tudou>
</item>
<item>
<title>This is title2</title>
<desc>This is desc2</desc>
<tudou>55362137</tudou>
</item>
<item>
<title>This is title3</title>
<desc>This is desc4</desc>
</item>
</items>
here's my php code:
<div class="nav">
<ul>
<?php
$xml = simplexml_load_file('post.xml');
//print_r($xml);
foreach($xml->item as $key=>$item )
{
echo <<<HTML
<li>
<div class="published">
<span class="day">13</span>
Sep 2010
</div>
<div class="summary">
<a href="#slide-$key">
<h3>$item->title</h3>
</a>
</div>
</li>
HTML;
}
?>
</ul>
</div>
after php render the page,the <a href="#slide-$key">
return <a href=#slide-item>
.
I want $key get the number of which ,
so how to get ?
解決方案
This is because simpleXML structures are not normal arrays but iterators who do not have an array index.
I don't know of any other solution than keeping track manually using a variable:
$index = 0;
$xml = simplexml_load_file('post.xml');
foreach($xml->item as $item )
{
....
$index++;
}
這篇關(guān)于在 simpleXMLElement 的 foreach 循環(huán)中獲取子索引的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!