PHP array_merge add to XML

Hello, I’m tryind to add array_merge ($itens = array_merge($item,$new) with attributes into XML , but it’s brings det nItem=“3” twice. why?

    <?php
      
      
    require 'conect_db_skymar.php';    
        
$new = array();   
 
 $queryInfC ="SELECT * FROM DBA.data_nfe_infCarga WHERE (nfe_id ='2007181826')";

    if( $result = odbc_exec ($conn, $queryInfC) ) {
    }
    while( $rowCar = odbc_fetch_array($result) ) {
            
   $item =  array(
      'name' =>  'det', 
      'attributes' => array(
      'nItem' => $rowCar ['det_nItem']
     ),
      array(
        'name' => 'prod',
        array (
         'name' => 'cProd',
         'value' => $rowCar ['cProd']
        ),
            
       array (
         'name' => 'cEAN',
         'value' => $rowCar ['cEAN'] 
        ),
          
     ) ,  
         
 );
           
    array_push($new, $item);
  
 }

print_r($new);
 
 $data = array(
        'name' => 'NFe', // "name" required, all else optional
        'attributes' => array(
        'xmlns' => 'http://www.portalfiscal.inf.br/nfe',
        ), 
        array(
            'name' => 'infNfe',
            'attributes' => array(
                'versao' => '4.00',
               'Id' => "NFe".$chaves,          
         ), 
         $itens = array_merge($item,$new),
              
        
) // <infCte>
); //</CTe>
 

$add_array = array(

);
   

function generate_xml_element( $dom, $data) {
    global $add_array;
    global $ide;

    
  
    if ( empty( $data['name'] ) )
        return false;
 
   
    if($data['name'] == 'ide'){
    $data = array_merge($data, $add_array);
    $data = array_merge($data); 
    }
    
 
    $element_value = (isset( $data['value'] ) ) ? $data['value'] : null;
    $element = $dom->createElement( $data['name'], $element_value );
      // Add any attributes
    if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) {
        foreach ( $data['attributes'] as $attribute_key => $attribute_value ) {
            $element->setAttribute( $attribute_key, $attribute_value );
          
        }
    }
 
    // Any other items in the data array should be child elements
    foreach ( $data as $data_key => $child_data ) {
        if ( ! is_numeric( $data_key ) )
            continue;
 
        $child = generate_xml_element( $dom, $child_data );
        if ( $child )
            $element->appendChild( $child );
    }
 
    return $element;
}

       
$doc = new DOMDocument('1.0','UTF-8');
$child = generate_xml_element( $doc, $data );
if ( $child )
    $doc->appendChild( $child );
$doc->formatOutput = true; // Add whitespace to make easier to read XML
$xml = $doc->saveXML();
$doc->save('add_xml.xml');
printf ("<pre>%s</pre>", htmlentities ($doc->saveXML()));

Results:

Array ( [0] => Array ( [name] => det [attributes] => Array ( [nItem] => 1 ) [0] => Array ( [name] => prod [0] => Array ( [name] => cProd [value] => IN020005 ) [1] => Array ( [name] => cEAN [value] => SEM GTIN ) ) ) [1] => Array ( [name] => det [attributes] => Array ( [nItem] => 2 ) [0] => Array ( [name] => prod [0] => Array ( [name] => cProd [value] => IN020006 ) [1] => Array ( [name] => cEAN [value] => SEM GTIN ) ) ) [2] => Array ( [name] => det [attributes] => Array ( [nItem] => 3 ) [0] => Array ( [name] => prod [0] => Array ( [name] => cProd [value] => IN020007 ) [1] => Array ( [name] => cEAN [value] => SEM GTIN ) ) ) )
<?xml version="1.0" encoding="UTF-8"?>
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
  <infNfe versao="4.00" Id="NFe">
    <det nItem="3">
      <prod>
        <cProd>IN020007</cProd>
        <cEAN>SEM GTIN</cEAN>
      </prod>
      <det nItem="1">
        <prod>
          <cProd>IN020005</cProd>
          <cEAN>SEM GTIN</cEAN>
        </prod>
      </det>
      <det nItem="2">
        <prod>
          <cProd>IN020006</cProd>
          <cEAN>SEM GTIN</cEAN>
        </prod>
      </det>
      <det nItem="3">
        <prod>
          <cProd>IN020007</cProd>
          <cEAN>SEM GTIN</cEAN>
        </prod>
      </det>
    </det>
  </infNfe>
</NFe>
Sponsor our Newsletter | Privacy Policy | Terms of Service