ThomasRedstone icon

Convert SimpleXML object to an array

ThomasRedstone | PRO | 10/26/13 11:54:15 PM UTC | 0 ⭐ | 244 👁️ | Never ⏰ | []
PHP |

2.35 KB

|

None

|

0 👍

/

0 👎

/**
* This function takes a simpleXML object, and, based on a configuration array, 
* extracts the required bits of data, in order to populate an array.
*/
function getArrayFromXml($results, $searchParams) {
        $id = 0;
        #echo '<h1>We have results: </h1><pre>'.print_r($results,1).'</pre>';
        if(!empty($results->searchResult->item)) {
            foreach($results->searchResult->item as $result) {
                foreach($searchParams as $param => $parampath) {
                    #echo "<h2>$id $param $parampath</h2>";
                    /**
                    * getChild is a function which takes a path to a sub-object and returns the value found there,
                    * for example, for the path 'results->shippingInfo->
                    $adjusted_result[$id][$param] = (string)
                            Utilities\Utilities::getChild($parampath,$result);
                    if(isset(Utilities\Configuration::$transformFields[$param])) {
                        $function = Utilities\Configuration::$transformFields[$param];
                        /**
                         * This section is eBay specific, but will not cause an
                         * issue for non-ebay searches.
                         * 
                         * Todo: work this into a generic process that is configured
                         * by the extending class.
                         */
                        if((isset($this->multicall) || $this->webservice == TRUE) && $param == 'timeLeft') {
                            $adjusted_result[$id][$param.'_seconds'] = \Utilities\Time::getSecondsLeftFromEbayTime($adjusted_result[$id][$param]);
                        }
                        $adjusted_result[$id][$param] = 
                                call_user_func($function,
                                        $adjusted_result[$id][$param]);
                    }
                }
                $id++;
            }
        }
        $adjusted_result['totalItems'] = (int) $results->paginationOutput->totalEntries;
        $adjusted_result['pageNumber'] = (int) $results->paginationOutput->pageNumber;
        $adjusted_result['totalPages'] = (int) $results->paginationOutput->totalPages;
        
        #echo "<h1>Adjusted Result:</h1>".print_r($adjusted_result,1).'</pre>';
        return $adjusted_result;
    }

Comments