2

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 
            ) 
        )
    )
)
5
  • 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? Commented Jun 9, 2010 at 18:33
  • 1
    Do you expect anyone to read your code? Please edit your question and format it properly. And then you can also put your real question into it. You can format your code as code by intending it with for spaces or marking it and press the 101 010 button. Commented Jun 9, 2010 at 18:37
  • yes,I tried a lot of stuff.I have formatted it . Commented Jun 9, 2010 at 18:40
  • Did it for you... this is what I'd call formatted. Commented Jun 9, 2010 at 18:43
  • thanks felix.i am new to it,but i'll get used to it. Commented Jun 9, 2010 at 18:58

1 Answer 1

4

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!

Sign up to request clarification or add additional context in comments.

6 Comments

+1 no need actually spell it out with such a clear manual page.
I want to associate both names with both id like foreach($xml->project as $project) { $name = (string) $project->name; $companies[$project->id]=$name; } }
@chirs: So? Then do it. What is your question?
@felix :I want to display both the names with their respective id as key.
@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?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.