4
<?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 ?

5 Answers 5

7

You could use SimpleXMLElement::xpath to return an array of the <item> elements, then access the array's keys as you want.

foreach ($xml->xpath('/items/item') as $key => $item) 
6

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++;
    }
1
  • I think you mean $key = 0; ($index would be a much better name). Oct 12, 2010 at 13:43
0

AFAIK with heredocs format, you need to wrap variables in curly braces:

<a href="#slide-{$key}">
  <h3>{$item->title}</h3>
</a>
1
  • Won't fix the issue I think: $key is item when walking through an XML Element
    – Pekka
    Oct 12, 2010 at 13:46
0

Heredoc

might try

<a href="#slide-{$key}">
0

You can use a for loop instead of a foreach like this :

$item = $xml->item;
$howMany = count($item);
for($i = 0; $i < $howMany; $i++){
echo <<<HTML
                    <li>
                    <div class="published">
                        <span class="day">13</span>
                        Sep 2010
                    </div>

                    <div class="summary">
                    <a href="#slide-$i">
                            <h3>$item->title</h3>
                    </a>
                    </div>
                    </li>

HTML;
}
1
  • Where is $item being fetched? I don't think this method will work for an XML element
    – Pekka
    Oct 12, 2010 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.