xpath olmadan simplexmlobject dizeden almak

0 Cevap php

Ben tekrar burada ... deliriyorum!

Ben bu SimpleXML dosya var:

SimpleXMLElement Object
(
    [PubmedArticle] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [MedlineCitation] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [Owner] => NLM
                                    [Status] => In-Process
                                )

                            [PMID] => 20538400
                            [DateCreated] => SimpleXMLElement Object
                                (
                                    [Year] => 2010
                                    [Month] => 07
                                    [Day] => 08
                                )

                            [Article] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [PubModel] => Print-Electronic
                                        )
etc.....

I want to have the "DateCreated" in this particular way: 2010-07-08 The following code works...:

foreach ($xml->xpath('//MedlineCitation') as $MedlineCitation)
{

    foreach ($MedlineCitation->DateCreated as $date)
    {
             foreach ($date as $ymd)
             {
             $datecreated .= $ymd . "-"; 
             }
     $datecreated = substr($datecreated, 0, -1);
     echo $datecreated;
     $datecreated = "";
     }
}

output: 2010-07-08 But... i want it without xpath, because i need to have another peace of code in this "foreach loop" that doesn't work with xpath...

Bu kod .... çalışmaz değildir:

foreach ($xml->PubmedArticle as $MedlineCitation)
{
       foreach ($MedlineCitation->DateCreated as $date)
       {
          foreach ($date as $ymd)
          {
          $datecreated .= $ymd . "-"; 
          }
        $datecreated = substr($datecreated, 0, -1);
        echo $datecreated;
        $datecreated = "";
        }
} 

Ve bu da ... wok değildir:

foreach ($xml->PubmedArticle as $MedlineCitation)
    {
    $datecreated = "";

    $datecreated = $MedlineCitation->DateCreated->Year;
    $datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Month;
    $datecreated = $datecreated . "-" . $MedlineCitation->DateCreated->Day;
    echo $datecreated;
    }

Best regards! Thijs

0 Cevap