When is an array not an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew C

    When is an array not an array?

    Hi, folks.

    I've encountered what seems to me to be something of an oddity while playing
    around with XML parsing in PHP, and I wondered if any of you might be able
    to clear up my confusion...

    Here's a little code:

    $xmlDoc = new DOMDocument();
    $xmlDoc->load('widget_d ata.xml');
    $widgets = $xmlDoc->getElementsByT agName('widget' );

    My understanding was that '$widgets' is an array of elements, and the
    following 'foreach' iterates through that array (this works):

    foreach ($widgets as $widget)
    {
    ....
    }

    However, I get an error if I try to access '$widgets' using square-brackets,
    e.g.:

    $widget = $widgets[0];

    (The error is: "Fatal error: Cannot use object of type DOMNodeList as
    array".)

    PHP confirms that the first element of the 'array' has an index/key of '0'
    with the following (or similar), but won't let me access it directly using
    '$widgets[0]':

    foreach ($widgets as $key =$widget)
    {
    echo("<p>" . $key . "</p>\n");
    }

    Does anyone have an explanation as to why a DOMNodeList can be accessed like
    an array using 'foreach', but won't allow square-brackets to be used? What
    exactly *is* a DOMNodeList?

    Thanks for any help.

    A.

    PS. I am aware that DOMNodeLists have an 'item()' method with which nodes
    within the list can be referred to via index, but my confusion still stands
    as to why I can't simply use '[]'s.


  • Andy Hassall

    #2
    Re: When is an array not an array?

    On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <nonsense@total ly.made.upwrote :
    >I've encountered what seems to me to be something of an oddity while playing
    >around with XML parsing in PHP, and I wondered if any of you might be able
    >to clear up my confusion...
    >
    >Here's a little code:
    >
    >$xmlDoc = new DOMDocument();
    >$xmlDoc->load('widget_d ata.xml');
    >$widgets = $xmlDoc->getElementsByT agName('widget' );
    >
    >My understanding was that '$widgets' is an array of elements,
    Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so you
    can use it in a foreach loop.
    >and the
    >following 'foreach' iterates through that array (this works):
    >
    >foreach ($widgets as $widget)
    >{
    >...
    >}
    >
    >However, I get an error if I try to access '$widgets' using square-brackets,
    >e.g.:
    >
    >$widget = $widgets[0];
    >
    >(The error is: "Fatal error: Cannot use object of type DOMNodeList as
    >array".)
    There you go.
    >PHP confirms that the first element of the 'array' has an index/key of '0'
    >with the following (or similar), but won't let me access it directly using
    >'$widgets[0]':
    >
    >foreach ($widgets as $key =$widget)
    >{
    >echo("<p>" . $key . "</p>\n");
    >}
    >
    >Does anyone have an explanation as to why a DOMNodeList can be accessed like
    >an array using 'foreach', but won't allow square-brackets to be used? What
    >exactly *is* a DOMNodeList?
    >
    >Thanks for any help.
    >
    >A.
    >
    >PS. I am aware that DOMNodeLists have an 'item()' method with which nodes
    >within the list can be referred to via index, but my confusion still stands
    >as to why I can't simply use '[]'s.
    PHP doesn't support overloading operators, which I believe would be required
    for supporting [] on objects implementing the appropriate interface.

    Would be a sensible extension to the current functionality, though - as you
    point out, DOMNodeList acts almost the same as an array, but not quite close
    enough.

    Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
    itself into an actual array, but it doesn't seem to have that either.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • whiskey

      #3
      Re: When is an array not an array?

      Does anyone have an explanation as to why a DOMNodeList can be accessed like
      an array using 'foreach', but won't allow square-brackets to be used? What
      exactly *is* a DOMNodeList?
      A DOMNodeList is an object - this is why you can't access items using
      square-brackets.
      Since PHP5 you can iterate through all visible properties of an object
      - this is why you can access items using a foreach statement.

      Comment

      • PleegWat

        #4
        Re: When is an array not an array?

        In article <q13ck2d3tc2204 gdsi3db9q0bds51 0pmsj@4ax.com>, Andy Hassall
        says...
        Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
        itself into an actual array, but it doesn't seem to have that either.
        If, as whiskey says, the nodes are properties of the object, you can do
        an explicit cast of the object to an array:

        $arrayofnodes = (array)$nodelis t;
        --
        PleegWat
        Remove caps to reply

        Comment

        • Andrew C

          #5
          Re: When is an array not an array?


          "Andy Hassall" <andy@andyh.co. ukwrote in message
          news:q13ck2d3tc 2204gdsi3db9q0b ds510pmsj@4ax.c om...
          On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <nonsense@total ly.made.up>
          wrote:
          >
          >>I've encountered what seems to me to be something of an oddity while
          >>playing
          >>around with XML parsing in PHP, and I wondered if any of you might be able
          >>to clear up my confusion...
          >>
          >>Here's a little code:
          >>
          >>$xmlDoc = new DOMDocument();
          >>$xmlDoc->load('widget_d ata.xml');
          >>$widgets = $xmlDoc->getElementsByT agName('widget' );
          >>
          >>My understanding was that '$widgets' is an array of elements,
          >
          Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so
          you
          can use it in a foreach loop.
          Thanks. That's my missing piece. I have quite a bit of experience of PHP
          prior to 5, but I'm just feeling my way with some of the 5 stuff. :-)

          A.


          Comment

          • whiskey

            #6
            Re: When is an array not an array?

            I dare say you can not cast a DOMNodeList to an array. Because, when
            converting an object to an array, you get the properties of the object.
            And DOMNodeList has only one porperty, which is "length" (and useless
            in this context).

            Anyway, I can't find any reason why one will ever need to convert a
            DOMNodeList to an array since it provides a method to retrieve a node
            specified by it's index ("item()"). Moreover, PHP5 offers the ability
            to iterate through objects. So why this fuss ?

            PleegWat, I never said nodes are properties. All I said was that PHP5
            adds the ability to iterate the visible properties of an object. But
            that's the default. You can customize the way PHP iterates through
            objects by implementing the "Iterator" or "IteratorAggreg ate"
            interfaces ;-)

            On Oct 31, 12:27 am, PleegWat
            <pleeg...@PLEEG WAT.leegwater-68.demon.nl.INV ALIDwrote:
            In article <q13ck2d3tc2204 gdsi3db9q0bds51 0p...@4ax.com>, Andy Hassall
            says...
            >
            Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
            itself into an actual array, but it doesn't seem to have that either.If, as whiskey says, the nodes are properties of the object, you can do
            an explicit cast of the object to an array:
            >
            $arrayofnodes = (array)$nodelis t;
            --
            PleegWat
            Remove caps to reply

            Comment

            • PleegWat

              #7
              Re: When is an array not an array?

              In article <1162285622.826 148.302460@m73g 2000cwd.googleg roups.com>,
              whiskey says...
              On Oct 31, 12:27 am, PleegWat
              <pleeg...@PLEEG WAT.leegwater-68.demon.nl.INV ALIDwrote:
              In article <q13ck2d3tc2204 gdsi3db9q0bds51 0p...@4ax.com>, Andy Hassall
              says...
              Failing that, it'd be somewhat useful if DOMNodeList had a method to
              convert itself into an actual array, but it doesn't seem to have that
              either.
              If, as whiskey says, the nodes are properties of the object, you can do
              an explicit cast of the object to an array:

              $arrayofnodes = (array)$nodelis t;
              >
              I dare say you can not cast a DOMNodeList to an array. Because, when
              converting an object to an array, you get the properties of the object.
              And DOMNodeList has only one porperty, which is "length" (and useless
              in this context).
              >
              Anyway, I can't find any reason why one will ever need to convert a
              DOMNodeList to an array since it provides a method to retrieve a node
              specified by it's index ("item()"). Moreover, PHP5 offers the ability
              to iterate through objects. So why this fuss ?
              >
              PleegWat, I never said nodes are properties. All I said was that PHP5
              adds the ability to iterate the visible properties of an object. But
              that's the default. You can customize the way PHP iterates through
              objects by implementing the "Iterator" or "IteratorAggreg ate"
              interfaces ;-)
              (top-posting fixed)

              Sorry, misunderstood you then. I did take a quick look at the manual but
              I couldn't find anything about hte DOMNodeList class.

              --
              PleegWat
              Remove caps to reply

              Comment

              • whiskey

                #8
                Re: When is an array not an array?



                Comment

                Working...