如何使用simplexml解析xml文件并在PHP中创建多维数组

时间:2022-08-27 18:04:21

I am trying to parse a xml file as below to create a multidimensional array .

我试图解析如下的xml文件来创建一个多维数组。

 <workbook  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<Worksheet ss:Name="45">
  <Table>
       <Row><Cell><Data>232</Data></Cell></Row>
   <Row><Cell><Data>Apple</Data></Cell></Row>
  </Table>
</Worksheet>
<Worksheet ss:Name="46 - new">
  <Table>
       <Row><Cell><Data>876</Data></Cell></Row>
   <Row><Cell><Data>samsung</Data></Cell></Row>
   </Table>
</Worksheet>

Here is the code thus far.

这是迄今为止的代码。

$xml=simplexml_load_file($xmlfile);
$result= array();
foreach($xml->Worksheet as $worksheet ) {
  $result['tab']['name'][]=$worksheet->attributes('ss', TRUE)->Name;
  foreach($worksheet as $table){
    foreach($table as $row){
      foreach($row as  $cell){
        $result['tab']['units'][]=$cell->Data;
      }
    }
  }
}

print_r($result);

I am trying to get array as below :

我想获得如下数组:

items =[
   tab=>[
   'name' => '45',
   'units'=>[
      ['0'=>'232'],
      ['1'=>'Apple']
   ]
   ]
   tab=>[
   'name' => '46-new',
   'units'=>[
      ['0'=>'876'],
      ['1'=>'samsung']
   ]
   ]
 ];

but i'm getting the result as below .

但我得到的结果如下。

array(
    [tab]=>array(
       [name]=>array
       (
          [0]=>SimpleXmlElement Object
           ( 
             [0]=> 45
           )
          [1]=>SimpleXmlElement object
           ( 
             [1]=>46-new
           )
       )
       [units]=>array
       (
          [0]=>SimpleXmlElement Object
           ( 
             [0]=> Nr
           )
          [1]=>SimpleXmlElement object
           ( 
             [0]=>model
           )
           [2]=>SimpleXmlElement Object
           ( 
             [0]=> 232
           )
          [3]=>SimpleXmlElement object
           ( 
             [0]=>apple
           ) 
           .........
       )

    )
)

it's quite challenging .How should the code be modified to achieve the goal?

这是非常具有挑战性的。如何修改代码以实现目标?

2 个解决方案

#1


1  

You could try to do the code as below:

您可以尝试执行以下代码:

$xmlfile = 'xmlfile.xml';
$xml=simplexml_load_file($xmlfile);
$result= array();
$i=0;
foreach($xml->Worksheet as $worksheet ) {
$i++;
$result['tab'][$i] = array();
$result['tab'][$i]['name']=(string) $worksheet->attributes("ss", true)->Name;
$pos=0;
$key = '';
 foreach($worksheet as $table){
    foreach($table as $row){
      foreach($row as  $cell){
        $result['tab'][$i]['units'][] = array( "" + $pos => (string) $cell->Data);
        $pos++;
      }
    }
  }
}

return $result;

#2


0  

You can't use the same key in your array (tab) more than once, so that will need to change to be a unique value.

您不能在阵列(选项卡)中多次使用相同的键,因此需要将其更改为唯一值。

Also, your xml layout doesn't lend itself well to looping through it like you have. If you assume that your table will always have 4 rows then you can make this work.

此外,您的xml布局不适合像您一样循环遍历它。如果您假设您的表总是有4行,那么您可以使其工作。

Try something like this...

试试这样的事......

$xml=simplexml_load_file($xmlfile);
$result= array();
foreach($xml->Worksheet as $key=>$worksheet ) {
  $result['tab'.($key+1)]['name']=$worksheet->attributes('ss', TRUE)->Name;
  foreach($worksheet as $table){
    $label1 = $table[0][0][0]; // row 0, cell 0, data 0
    $label2 = $table[1][0][0]; // row 1, cell 0, data 0
    $value1 = $table[2][0][0]; // row 2, cell 0, data 0
    $value2 = $table[3][0][0]; // row 3, cell 0, data 0
    $result['tab'.($key+1)]['units'][$label1]=$value1;
    $result['tab'.($key+1)]['units'][$label2]=$value2;
  }
}

print_r($result);

#1


1  

You could try to do the code as below:

您可以尝试执行以下代码:

$xmlfile = 'xmlfile.xml';
$xml=simplexml_load_file($xmlfile);
$result= array();
$i=0;
foreach($xml->Worksheet as $worksheet ) {
$i++;
$result['tab'][$i] = array();
$result['tab'][$i]['name']=(string) $worksheet->attributes("ss", true)->Name;
$pos=0;
$key = '';
 foreach($worksheet as $table){
    foreach($table as $row){
      foreach($row as  $cell){
        $result['tab'][$i]['units'][] = array( "" + $pos => (string) $cell->Data);
        $pos++;
      }
    }
  }
}

return $result;

#2


0  

You can't use the same key in your array (tab) more than once, so that will need to change to be a unique value.

您不能在阵列(选项卡)中多次使用相同的键,因此需要将其更改为唯一值。

Also, your xml layout doesn't lend itself well to looping through it like you have. If you assume that your table will always have 4 rows then you can make this work.

此外,您的xml布局不适合像您一样循环遍历它。如果您假设您的表总是有4行,那么您可以使其工作。

Try something like this...

试试这样的事......

$xml=simplexml_load_file($xmlfile);
$result= array();
foreach($xml->Worksheet as $key=>$worksheet ) {
  $result['tab'.($key+1)]['name']=$worksheet->attributes('ss', TRUE)->Name;
  foreach($worksheet as $table){
    $label1 = $table[0][0][0]; // row 0, cell 0, data 0
    $label2 = $table[1][0][0]; // row 1, cell 0, data 0
    $value1 = $table[2][0][0]; // row 2, cell 0, data 0
    $value2 = $table[3][0][0]; // row 3, cell 0, data 0
    $result['tab'.($key+1)]['units'][$label1]=$value1;
    $result['tab'.($key+1)]['units'][$label2]=$value2;
  }
}

print_r($result);