I have a problem trying to handle SimpleXml objects.  Though I can manage
some workaround the problem, I would like to understand it better.

This is a section of the XML file I'm trying to read (it is not complete,
just a section)

             <tr>
               <td>BERMUDA</td>
               <td>Bermudian Dollar (customarily
                 known as Bermuda Dollar) </td>
               <td>BMD</td>
               <td>060</td>
             </tr>
             <tr>
               <td>BHUTAN</td>
               <td>
                 <p>Indian Rupee</p>
                 <p>Ngultrum</p>
               </td>
               <td>
                 <p>INR</p>
                 <p>BTN</p>
               </td>
               <td>
                 <p>356</p>
                 <p>064</p>
               </td>
             </tr>

It is part of a table with currency codes.   Each row has 4 cells containing
the country name, the currency description, the currency code and the
numeric code, as seen in the first row.  Nevertheless, with countries that
have more than one currency, it still has a single row with the first cell
containing the country name (see BHUTAN) but then in the following cells it
has several lines, one for each currency, as simple paragraphs.

When I get to the row for BHUTAN, I do a foreach to see what's in each cell.
Function dump() just does a var_dump() of whatever is given in the first
argument within a <pre> tag and uses the second argument as a heading,
surrounded by dashes.  In this case I copied the expression of what I am
dumping as the title (within single quotes so it does not expand).

 foreach($row as $cell) {
   dump($cell,'$cell);
   dump($cell->p,'$cell->p');
   dump((array)$cell->p,'(array)$cell->p');
   dump($cell->p[1],'$cell->p[1]');
  }

The section in question is shown below.  Though $cell is shown as a
SimpleXMLElement containing one 'p' property which contains an array of two
elements:
----------------------------$cell-----------------
object(SimpleXMLElement)#8 (1) {
 ["p"]=>
 array(2) {
   [0]=>
   string(12) "Indian Rupee"
   [1]=>
   string(8) "Ngultrum"
 }
}


If I dump that 'p' element, I don't get an array but an object with just
one element, the first value in the array
----------------------------$cell->p-----------------
object(SimpleXMLElement)#7 (1) {
 [0]=>
 string(12) "Indian Rupee"
}


If I typecast it to an array, I still get a single element
----------------------------(array)$cell->p-----------------
array(1) {
 [0]=>
 string(12) "Indian Rupee"
}

Nevertheless, if I explicitly index into the 'p' element, without even
typecasting it into an array, the second element is there.
----------------------------$cell->p[1]-----------------
object(SimpleXMLElement)#10 (1) {
 [0]=>
 string(8) "Ngultrum"
}


So, now you see it, now you don't, or do you?  What's happening?

Satyam

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to