Re: Array and Object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 24, 2008 at 11:23 AM, VamVan <vamseevan@xxxxxxxxx> wrote:

> Well anyways please let me handle the problems with decoding. I just
> want to know if there is a way that I could traverse through all the
> elements of the simplexmlobject and still keep the same structure of
> the array. What I mean is
>
>  [Advanced Virtualization Technologies in Workstation 6-20080320 1604]
> => SimpleXMLElement Object
>       (
>           [recordingID] => 932467
>           [hostWebExID] => marketingprograms
>           [name] => Title
>           [createTime] => 03/20/2008 09:04:42
>           [timeZoneID] => 4
>           [size] => 49.441048
>            [recordingType] => 0
>           [duration] => 3218
>       )
>
> Go through all the elements of the object and do some encoding and get
> back the same array structure.
>
> In raw terms I need to know the procedure that does this and still
> keep the same structure of the array.
>
> [Advanced Virtualization Technologies in Workstation 6-20080320 1604]
> => SimpleXMLElement Object
>       (
>            [recordingID] => utf8_decode(932467)
>           [hostWebExID] => utf8_decode(marketingprograms)
>           [name] => utf8_decode(Title)
>           [createTime] => utf8_decode(03/20/2008 09:04:42)
>           [timeZoneID] => utf8_decode(4)
>           [size] => utf8_decode(49.441048)
>            [recordingType] => utf8_decode(0)
>           [duration] => utf8_decode(3218)
>       )


first of all, SimpleXMLElement is not an array, its a class.  It does
support iteration via the foreach construct though.  the foreach iteration
will only go through the first set of elements in the structure, that is it
will only cover the children of the root element.  if you want to traverse
all elements of the structure recursively, use SimpleXMLIterator (and
RecursiveIteratorIterator) from SPL.

heres a simple xample to get you started:

<?php
$xml = '<m><a><b/></a><c/></m>';
$m = new SimpleXMLElement($xml);
foreach($m as $k => $v) { echo "k:$k => v:$v" . PHP_EOL; }
/* outputs
k:a => v:
k:c => v:
*/
$m = new SimpleXMLIterator($xml);
foreach(new RecursiveIteratorIterator($m,
RecursiveIteratorIterator::CHILD_FIRST) as $k => $v) { echo "k:$k => v:$v" .
PHP_EOL; }
/* outputs
k:b => v:
k:a => v:
k:c => v:
*/
-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux