简体   繁体   中英

Where is null coming from in this array?

I am converting a php array into xml with something like this :

$bigArray = $readConnection->fetchAll($query);

  $doc = new DOMDocument();
  $doc->formatOutput = true;       
  $r = $doc->createElement( "DATA" );
  $doc->appendChild( $r );     
  foreach( $bigArray as $product )
  {
    $b = $doc->createElement( "ITEM" );        
    $product_type = $doc->createElement( "PRODUCT_TYPE" );
    $product_type->appendChild(
    $doc->createTextNode( $product['ProductType'] )
    );
    $b->appendChild( $product_type ); 
    $sku = $doc->createElement( "SKU" );
    $sku->appendChild(
    $doc->createTextNode( $product['SKU'] )
    );
    $b->appendChild( $sku ); 
    $r->appendChild( $b );
   }

  echo $doc->saveXML();

This returns an xml doc however at the very end null is being appended and I think that is what is causing me other problems. So for example at the bottom of the xml that is output it looks like :

  </ITEM>
</DATA>
null

This null value is coming from the original array I see if I do:

print_r($bigArray)

I see something like :

Array ( [0] => Array ( [ProductType] => simple [SKU] => 09423100010018 ) [1] => Array ( [ProductType] => simple [SKU] => 14552300010002 )) null

I am calling this from a class in magento like :

class Foo_Model_Queryone extends Mage_Core_Model_Abstract
{

    public function pprQuery() {
    $resource = Mage::getSingleton('core/resource');    
    $readConnection = $resource->getConnection('core_read');    
    $query = ("SELECT cpe.type_id AS 'ProductType',
      cpe.sku AS 'SKU',
      .....

The Class Mage_Core_Model_Abstract is causing the problem, or another class higher up, that is outputting the null value.

Try to step through it using Xdebug in Netbeans, and see what is causing the null value. I am interested to see the outcome.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM