Advertisement
ThomasRedstone

Convert SimpleXML object to an array

Oct 26th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. /**
  2. * This function takes a simpleXML object, and, based on a configuration array,
  3. * extracts the required bits of data, in order to populate an array.
  4. */
  5. function getArrayFromXml($results, $searchParams) {
  6.         $id = 0;
  7.         #echo '<h1>We have results: </h1><pre>'.print_r($results,1).'</pre>';
  8.        if(!empty($results->searchResult->item)) {
  9.             foreach($results->searchResult->item as $result) {
  10.                 foreach($searchParams as $param => $parampath) {
  11.                     #echo "<h2>$id $param $parampath</h2>";
  12.                    /**
  13.                     * getChild is a function which takes a path to a sub-object and returns the value found there,
  14.                     * for example, for the path 'results->shippingInfo->
  15.                     $adjusted_result[$id][$param] = (string)
  16.                             Utilities\Utilities::getChild($parampath,$result);
  17.                     if(isset(Utilities\Configuration::$transformFields[$param])) {
  18.                         $function = Utilities\Configuration::$transformFields[$param];
  19.                         /**
  20.                          * This section is eBay specific, but will not cause an
  21.                          * issue for non-ebay searches.
  22.                          *
  23.                          * Todo: work this into a generic process that is configured
  24.                          * by the extending class.
  25.                          */
  26.                         if((isset($this->multicall) || $this->webservice == TRUE) && $param == 'timeLeft') {
  27.                             $adjusted_result[$id][$param.'_seconds'] = \Utilities\Time::getSecondsLeftFromEbayTime($adjusted_result[$id][$param]);
  28.                         }
  29.                         $adjusted_result[$id][$param] =
  30.                                 call_user_func($function,
  31.                                         $adjusted_result[$id][$param]);
  32.                     }
  33.                 }
  34.                 $id++;
  35.             }
  36.         }
  37.         $adjusted_result['totalItems'] = (int) $results->paginationOutput->totalEntries;
  38.         $adjusted_result['pageNumber'] = (int) $results->paginationOutput->pageNumber;
  39.         $adjusted_result['totalPages'] = (int) $results->paginationOutput->totalPages;
  40.        
  41.         #echo "<h1>Adjusted Result:</h1>".print_r($adjusted_result,1).'</pre>';
  42.        return $adjusted_result;
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement