-- 
Edd Dumbill.  More from me at <http://usefulinc.com/edd/blog>
Managing Editor, XML.com, XMLhack.com; Chair, XML Europe 2003

--- Begin Message --- Hello!

Many thanks for providing an excellent library.

I modified the xmlrpc_encode() function to distinguish between associative and sequential arrays. I also re-wrote it to avoid using the gettype() function, which, according to php.net, should now be avoided.

I have included the changes below, in case you are interested in seeing what I did.

Thanks again!



/****************************************************************
* xmlrpc_encode takes native php types and encodes them into    *
* xmlrpc PHP object format.                                     *
*                                                               *
* BUG: Differentiation between associative and sequential       *
* arrays is not perfect.                                        *
*                                                               *
* feature creep -- could support more types via optional type   *
* argument.                                                     *
*                                                               *
* author: Dan Libby ([EMAIL PROTECTED])                             *
* modified 2003 08 22: Mike van Lammeren ([EMAIL PROTECTED])     *
****************************************************************/
function xmlrpc_encode($php_val)
{
   global $xmlrpcInt;
   global $xmlrpcDouble;
   global $xmlrpcString;
   global $xmlrpcBoolean;
   global $xmlrpcStruct;
   global $xmlrpcArray;

$xmlrpc_val = new xmlrpcval;

   if( is_integer( $php_val ) ) { # integer
      $xmlrpc_val->addScalar($php_val, $xmlrpcInt);
   }
   elseif( is_float( $php_val ) ){ # double / float
         $xmlrpc_val->addScalar($php_val, $xmlrpcDouble);
   }
   elseif( is_string( $php_val ) ){ # string
         $xmlrpc_val->addScalar($php_val, $xmlrpcString);
   }
   elseif( is_bool( $php_val ) ){ # boolean
         $xmlrpc_val->addScalar($php_val, $xmlrpcBoolean);
   }
   elseif( is_object( $php_val ) or is_assoc_array( $php_val ) ){ # struct
      $assoc = array();
      reset( $php_val );
      while( list( $k, $v ) = each( $php_val ) ) {
         $assoc[$k] = xmlrpc_encode($v);
      }
      $xmlrpc_val->addStruct($assoc);
   }
   elseif( is_array( $php_val ) ){ # array
      $arr = array();
      foreach( $php_val as $val ) {
         $arr[] = xmlrpc_encode( $val );
      }
      $xmlrpc_val->addArray($arr);
   }
   else{
      # empty object
   }

   return $xmlrpc_val;
}



/****************************************************************
* is_assoc_array tries to decide whether or not a given array   *
* is an associative array, or a sequential array. Of course, no *
* such distinction is made by PHP, so it really just tests      *
* whether or not a given array could possibly be a sequential   *
* array. Since an associative array with sequential, integer    *
* keys 'looks' just like a sequential array, this function will *
* be fooled.                                                    *
*                                                               *
* BUG: Associative arrays with sequential, integer keys 'look'  *
* just like sequential arrays, and will be identified as such.  *
*                                                               *
* date: 2003 08 22                                              *
* author: Mike van Lammeren ([EMAIL PROTECTED])                  *
****************************************************************/
function is_assoc_array( $php_val ) {
   if( !is_array( $php_val ) ){
      # Neither an associative, nor non-associative array.
      return false;
   }

   $given_keys = array_keys( $php_val );
   $non_assoc_keys = range( 0, count( $php_val ) );

if( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
if( array_diff_assoc( $given_keys, $non_assoc_keys ) ){
return true;
}
else {
return false;
}
}
else {
if( array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys ) ){
return true;
}
else {
return false;
}
}
}




--
Mike van Lammeren
Web Application Developer
Emerge2 Digital Inc., "evolving business to digital"
Web: http://www.emerge2.com Email: [EMAIL PROTECTED]


--- End Message ---
_______________________________________________
phpxmlrpc mailing list
[EMAIL PROTECTED]
http://lists.usefulinc.com/cgi-bin/mailman/listinfo/phpxmlrpc

Reply via email to