tags:

views:

43

answers:

1

I have a simpleXml object and want to read the data from the object.I am new to php.The object details are as follows.I want to read name like general and name which is inside company array i.e Korey Kay & Partners.What is the syntax for it?

SimpleXMLElement Object ( 
    [@attributes] => Array ( [type] => array ) 
    [project] => Array (  
        [0] => SimpleXMLElement Object ( 
            [created-on] => 2008-07-18 
            [id] => 2257372 
            [last-changed-on] => 2010-05-27T22:28:29Z 
            [name] => *GENERAL 
            [status] => active 
            [company] => SimpleXMLElement Object ( 
                 [id] => 406952 
                 [name] => Korey Kay & Partners 
            ) 
        )
    )
)
+3  A: 

The documentation offers some examples. I think it is very well explained.

For looping you can use for or foreach.


Because it is your first question ;) In your case it would be something like:

$projects = array();
$companies = array();

foreach($xml->project as $project) {
    $projects[$project->id] = $project->name;
    $companies[$project->company->id] = $project->company->name;
    // and / or
    echo 'Project ' . $project->name . ' has ID ' . $project->id . PHP_EOL;
    echo 'Company ' . $project->company->name . ' has ID ' . $project->company->id . PHP_EOL;
}

PHP's documentation is quite good imho. For the really basic elements, they offer good examples. I very much advice you to read it!

Felix Kling
+1 The way to go as per question details :)
Sarfraz
+1 no need actually spell it out with such a clear manual page.
Wrikken
I want to associate both names with both id like foreach($xml->project as $project) { $name = (string) $project->name; $companies[$project->id]=$name;}}
chirs
@chirs: So? Then do it. What is your question?
Felix Kling
@felix :I want to display both the names with their respective id as key.
chirs
@chirs: See my updated code. If you want to display it, then just echo also the IDs. Maybe you should read a PHP tutorial before?
Felix Kling
@felix.It has helped me.Thank you
chirs
@felix .How do you format the code?
chirs
@chirs: Have a look here: http://stackoverflow.com/editing-help
Felix Kling