2017-05-16 8 views
0

にSimpleXMLElementオブジェクトを変換するIは、次のようにXMLファイルの内容を有する:PHPによって読み取りXML及びアレイ

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"> 
    <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"></OfficeDocumentSettings> 
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"></ExcelWorkbook> 
    <Worksheet ss:Name="Sheet 1"> 
     <Table> 
      <Row> 
       <Cell> 
        <Data ss:Type="String">store</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">websites</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">attribute_set</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">type</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">category_ids</Data> 
       </Cell> 
      </Row> 
      <Row> 
       <Cell> 
        <Data ss:Type="String">admin</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">base</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">Books</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">simple</Data> 
       </Cell> 
       <Cell> 
        <Data ss:Type="String">2,4,276,280</Data> 
       </Cell> 
      </row> 
     </Table> 
    </Worksheet> 
</Workbook> 

Iは、配列要素として各列を変換しなければなりません。このために私が使用しています:

$xml = simplexml_load_file('list_product.xml'); 

私は行の配列を取得することができていますが、細胞のために、私は唯一のSimpleXMLElementオブジェクトを取得しています。

+0

それが配列であることを_have_ないのはなぜ?あなたは配列で必要なオブジェクトで何ができませんか? – Tom

+0

http://stackoverflow.com/a/6167346/916000こちらをご覧ください。 –

答えて

0

このような用途の簡単な関数:

$output = null; 
function simpleXmlElementToArray(&$output, \SimpleXmlElement $element) 
{ 
    $output = (array)$element; 
    foreach ($output as $k => $v) { 
     if ($v instanceof \SimpleXMLElement) { 
      simpleXmlElementToArray($output[$k], $v); 
     } 
    } 
} 
0
function xmlToArray($xml){ 
    $xml = str_replace(array("\n", "\r", "\t"), '', $xml); 
    // $xml = trim(str_replace('"', "'", $xml)); 
    $xml = trim($xml); 
    $start_tree = (array) simplexml_load_string($xml); 

    $final_tree = array(); 
    loopRecursivelyForAttributes($start_tree, $final_tree); 
    return $final_tree; 
} 

function loopRecursivelyForAttributes($start_tree, &$final_tree){ 
    foreach($start_tree as $key1 => $row1){ 
     if(!array_key_exists($key1, $final_tree)){ 
      $final_tree[$key1] = array(); 
     } 

     // If there is only one sub node, then there will be one less 
     // array - ie: $row1 will be an array which has an '@attributes' key 
     if(array_key_exists('@attributes', $row1)){ 
      $row1 = (array) $row1; 
      getValues($start_tree, $final_tree, $key1, $row1); 
     } 
     else{ 
      foreach ($row1 as $row2){ 
       $row2 = (array) $row2; 
       getValues($start_tree, $final_tree, $key1, $row2); 
      } 
     } 
    } 
} 

function getValues($start_tree, &$final_tree, $key1, $row2){ 
    foreach ($row2 as $key3 => $val3){ 
     $val3 = (array) $val3; 

     if($key3 == '@attributes'){ 
      $final_tree[$key1][] = $val3; 
     } 
     else{ 
      $temp_parent = array(); 
      $temp_parent[$key3] = $val3; 
      loopRecursivelyForAttributes($temp_parent, $final_tree[$key1][count($final_tree[$key1]) - 1]); 
     } 
    } 
} 

//usage 
$xml = xmlToArray('xml as string'); 
関連する問題