2016-07-19 4 views
-1

Ich möchte jede Coa zu array.here ist der XML-Dateicode.Wie xml Objekt in Array zu schieben?

<?xml version="1.0" encoding="utf-8"?> 
<companies> 
    <company> 
     <name>Best Western</name> 
     <groups> 
      <group> 
       <coa>410101 · Net Room Revenue Taxable</coa> 
       <coa>415101 · GTD No Show</coa> 
       <coa>Total 400000 · ROOM REVENUE</coa> 
       <coa>Total I · ROOM REVENUE</coa> 
       <coa>II · OTHER OPERATING REVENUE</coa> 
       <coa>425120 · Meeting Room</coa> 
       <coa>480380 · Interest Income</coa> 
       <coa>480383 · Guest Laundry</coa> 
       <coa>480385 · Vending</coa> 
       <coa>480389 · Miscellaneous</coa> 
       <coa>482000 · Sales Tax Discounts</coa> 
       <coa>Total 480000 · OTHER INCOME</coa> 
       <coa>Total II · OTHER OPERATING REVENUE</coa> 
       <coa>Total Income</coa> 
      </group> 
      <group> 
       <coa>10 · ROOMS DEPARTMENT</coa> 
       <coa>Total Income</coa> 
      </group> 
     </groups> 
    </company> 
</companies> 

Jetzt habe ich diesen Code versucht

$xml = simplexml_load_file($url); 
foreach ($xml as $company) { 
$groups = $company->groups; 
    $coa_list = $groups->group; 
    $coa_array = array(); 
    foreach ($coa_list as $coal) { 
     $coa_array = array(); 
     $coa_k = $coal->coa; 
     foreach ($coa_k as $key => $value) { 
      $val = $value; 
      array_push($coa_array, $val); 
     } 
    } 
} 

Eigentlich sollte es funktionieren, aber ich weiß nicht, warum, aber es funktioniert nicht.

Hinweis: Ich lese die XML-Datei.

Wenn ich $ val vor dem Array Push drucke, wird es gedruckt, aber kann es nicht in Array schieben.

So können Sie mir empfehlen, was das Problem mit meinem Code ist

Dank

+2

Sie befinden sich in jeder Schleife das Array zurückzusetzen. Du willst das nicht tun. Definiere '$ coa_array = array();' außerhalb der 'foreach' – Andrew

+0

Ich habe das gemacht Aber dasselbe Ergebnis –

+0

Willst du alle' coa' Werte aus jeder Gruppe in einem Array? Oder möchten Sie, dass Coas aus der gleichen Gruppe innerhalb ihres eigenen Arrays liegen? – BeetleJuice

Antwort

0

Datei example.xml

<?xml version="1.0" encoding="utf-8"?> 
<companies> 
    <company> 
     <name>Best Western</name> 
     <groups> 
      <group> 
       <coa>410101 · Net Room Revenue Taxable</coa> 
       <coa>415101 · GTD No Show</coa> 
       <coa>Total 400000 · ROOM REVENUE</coa> 
       <coa>Total I · ROOM REVENUE</coa> 
       <coa>II · OTHER OPERATING REVENUE</coa> 
       <coa>425120 · Meeting Room</coa> 
       <coa>480380 · Interest Income</coa> 
       <coa>480383 · Guest Laundry</coa> 
       <coa>480385 · Vending</coa> 
       <coa>480389 · Miscellaneous</coa> 
       <coa>482000 · Sales Tax Discounts</coa> 
       <coa>Total 480000 · OTHER INCOME</coa> 
       <coa>Total II · OTHER OPERATING REVENUE</coa> 
       <coa>Total Income</coa> 
      </group> 
      <group> 
       <coa>10 · ROOMS DEPARTMENT</coa> 
       <coa>Total Income</coa> 
      </group> 
     </groups> 
    </company> 
</companies> 

Hier ist der PHP-Code zum Holen von Coa für dich ha um das simplexmlobject in Array zu konvertieren.

<?php 
    $xml = simplexml_load_file('example.xml'); 

    // echo "<pre>"; 
    // print_r($xml); 
    // echo "</pre>"; 

    $coa_array = array(); 
    foreach ($xml as $company) { 
     $groups = $company->groups; 
     $coa_list = $groups->group;  
     foreach ($coa_list as $coal) { 
      $coa_k = $coal->coa; 
      foreach ($coa_k as $key => $value) { 
       $val = xml2array($value); 
       // echo "<pre>"; 
       // print_r($val[0]); 
       // echo "</pre>"; 
       // $val[] 
       array_push($coa_array, $val[0]); 
      } 
     } 
    } 

    echo "<pre>"; 
    print_r($coa_array); 
    echo "</pre>"; 

    function xml2array($xmlObject, $out = array()){ 
     foreach ((array) $xmlObject as $index => $node) 
      $out[$index] = (is_object ($node)) ? xml2array ($node) : $node; 

     return $out; 
    } 
?> 

Ergebnis sein wird

Array 
(
    [0] => 410101 · Net Room Revenue Taxable 
    [1] => 415101 · GTD No Show 
    [2] => Total 400000 · ROOM REVENUE 
    [3] => Total I · ROOM REVENUE 
    [4] => II · OTHER OPERATING REVENUE 
    [5] => 425120 · Meeting Room 
    [6] => 480380 · Interest Income 
    [7] => 480383 · Guest Laundry 
    [8] => 480385 · Vending 
    [9] => 480389 · Miscellaneous 
    [10] => 482000 · Sales Tax Discounts 
    [11] => Total 480000 · OTHER INCOME 
    [12] => Total II · OTHER OPERATING REVENUE 
    [13] => Total Income 
    [14] => 10 · ROOMS DEPARTMENT 
    [15] => Total Income 
) 
0

Problem ist mit dem Array declaration.because jedes Mal wird es gleiche Array mit dem gleichen Namen für Schleife seit seinem Inneren schaffen, machen Array-Deklaration dynamische

0

Sie müssen die Array-Deklarationen außerhalb des Codes verschieben:

Datei test.xml

<?xml version="1.0" encoding="utf-8"?> 
<companies> 
    <company> 
     <name>Best Western</name> 
     <groups> 
      <group> 
       <coa>410101 · Net Room Revenue Taxable</coa> 
       <coa>415101 · GTD No Show</coa> 
       <coa>Total 400000 · ROOM REVENUE</coa> 
       <coa>Total I · ROOM REVENUE</coa> 
       <coa>II · OTHER OPERATING REVENUE</coa> 
       <coa>425120 · Meeting Room</coa> 
       <coa>480380 · Interest Income</coa> 
       <coa>480383 · Guest Laundry</coa> 
       <coa>480385 · Vending</coa> 
       <coa>480389 · Miscellaneous</coa> 
       <coa>482000 · Sales Tax Discounts</coa> 
       <coa>Total 480000 · OTHER INCOME</coa> 
       <coa>Total II · OTHER OPERATING REVENUE</coa> 
       <coa>Total Income</coa> 
      </group> 
      <group> 
       <coa>10 · ROOMS DEPARTMENT</coa> 
       <coa>Total Income</coa> 
      </group> 
     </groups> 
    </company> 
</companies> 

Der PHP-Code

<?php 
$xml = simplexml_load_file("test.xml"); 

$coa_array = array(); 
foreach ($xml as $company) { 
    $groups = $company->groups; 
    $coa_list = $groups->group;  
    foreach ($coa_list as $coal) { 
     $coa_k = $coal->coa; 
     foreach ($coa_k as $key => $value) { 
      $val = $value; 
      array_push($coa_array, $val); 
     } 
    } 
} 

print_r($coa_array); 

Drucke:

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [0] => 410101 · Net Room Revenue Taxable 
     ) 

    [1] => SimpleXMLElement Object 
     (
      [0] => 415101 · GTD No Show 
     ) 

    [2] => SimpleXMLElement Object 
     (
      [0] => Total 400000 · ROOM REVENUE 
     ) 

    [3] => SimpleXMLElement Object 
     (
      [0] => Total I · ROOM REVENUE 
     ) 

    [4] => SimpleXMLElement Object 
     (
      [0] => II · OTHER OPERATING REVENUE 
     ) 

    [5] => SimpleXMLElement Object 
     (
      [0] => 425120 · Meeting Room 
     ) 

    [6] => SimpleXMLElement Object 
     (
      [0] => 480380 · Interest Income 
     ) 

    [7] => SimpleXMLElement Object 
     (
      [0] => 480383 · Guest Laundry 
     ) 

    [8] => SimpleXMLElement Object 
     (
      [0] => 480385 · Vending 
     ) 

    [9] => SimpleXMLElement Object 
     (
      [0] => 480389 · Miscellaneous 
     ) 

    [10] => SimpleXMLElement Object 
     (
      [0] => 482000 · Sales Tax Discounts 
     ) 

    [11] => SimpleXMLElement Object 
     (
      [0] => Total 480000 · OTHER INCOME 
     ) 

    [12] => SimpleXMLElement Object 
     (
      [0] => Total II · OTHER OPERATING REVENUE 
     ) 

    [13] => SimpleXMLElement Object 
     (
      [0] => Total Income 
     ) 

    [14] => SimpleXMLElement Object 
     (
      [0] => 10 · ROOMS DEPARTMENT 
     ) 

    [15] => SimpleXMLElement Object 
     (
      [0] => Total Income 
     ) 

) 

Wenn Sie jede Gruppe einen neuen Eintrag in der Array erstellen haben möchten, können Sie den Code wie folgt ändern:

<?php 
$xml = simplexml_load_file("test.xml"); 

$group_array = array(); 
foreach ($xml as $company) { 
    $groups = $company->groups; 
    $coa_list = $groups->group;  
    foreach ($coa_list as $coal) { 
     $coa_k = $coal->coa; 
     $coa_array = []; 
     foreach ($coa_k as $key => $value) { 
      $val = $value; 
      array_push($coa_array, $val); 
     } 
     array_push($group_array, $coa_array); 
    } 
} 

print_r($group_array); 

Drucke:

Array 
(
    [0] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [0] => 410101 · Net Room Revenue Taxable 
       ) 

      [1] => SimpleXMLElement Object 
       (
        [0] => 415101 · GTD No Show 
       ) 

      [2] => SimpleXMLElement Object 
       (
        [0] => Total 400000 · ROOM REVENUE 
       ) 

      [3] => SimpleXMLElement Object 
       (
        [0] => Total I · ROOM REVENUE 
       ) 

      [4] => SimpleXMLElement Object 
       (
        [0] => II · OTHER OPERATING REVENUE 
       ) 

      [5] => SimpleXMLElement Object 
       (
        [0] => 425120 · Meeting Room 
       ) 

      [6] => SimpleXMLElement Object 
       (
        [0] => 480380 · Interest Income 
       ) 

      [7] => SimpleXMLElement Object 
       (
        [0] => 480383 · Guest Laundry 
       ) 

      [8] => SimpleXMLElement Object 
       (
        [0] => 480385 · Vending 
       ) 

      [9] => SimpleXMLElement Object 
       (
        [0] => 480389 · Miscellaneous 
       ) 

      [10] => SimpleXMLElement Object 
       (
        [0] => 482000 · Sales Tax Discounts 
       ) 

      [11] => SimpleXMLElement Object 
       (
        [0] => Total 480000 · OTHER INCOME 
       ) 

      [12] => SimpleXMLElement Object 
       (
        [0] => Total II · OTHER OPERATING REVENUE 
       ) 

      [13] => SimpleXMLElement Object 
       (
        [0] => Total Income 
       ) 

     ) 

    [1] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [0] => 10 · ROOMS DEPARTMENT 
       ) 

      [1] => SimpleXMLElement Object 
       (
        [0] => Total Income 
       ) 

     ) 

) 
1

Cooler Trick: json_encode SimpleXML Objekte kodieren können, und json_decode verwendet werden, können sie in einem Array zu verwandeln:

$json = json_encode(simplexml_load_string($url)); 
$array = json_decode($json,true); 
$coaGroups = array_column($array['company']['groups']['group'],'coa'); 

Live demo

+0

Hervorragende Lösung so viel Code in nur drei Zeile :) Kudos. –

+0

Danke für die freundlichen Worte. Froh, dass es geholfen hat. Wenn es Ihre gewählte Lösung ist, dann wählen Sie sie als Ihre Antwort aus. Wenn nicht, wählen Sie einen anderen. – BeetleJuice