0

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

 <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 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

what do you want to achieeve by these changes?...atleast i was getting something with my code . can you explain it plz
Sorry @nohan, i forgot to put $i in the inner foreach. There is a need to cast the element to string, I am not sure if the cast to string I put now is the most correct way to do too, but try and tell me.
Sorry again, I fixed now, try again.
I changed the cod to do exactly as you expect. If you like the solution don´t forget to vote in that, please ;).
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.

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);

2 Comments

table has hundreds of rows :(
Can you change the format of the table? It seems strange that the format is row 1 - label, row 2 - label, row 3 - value for row 1, row 4 - value for row2

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.