/** * Converts a simpleXML element into an array. Preserves attributes and everything. * You can choose to get your elements either flattened, or stored in a custom index that * you define. * For example, for a given element * <field name="someName" type="someType"/> * if you choose to flatten attributes, you would get: * $array['field']['name'] = 'someName'; * $array['field']['type'] = 'someType'; * If you choose not to flatten, you get: * $array['field']['@attributes']['name'] = 'someName'; * _____________________________________ * Repeating fields are stored in indexed arrays. so for a markup such as: * <parent> * <child>a</child> * <child>b</child> * <child>c</child> * </parent> * you array would be: * $array['parent']['child'][0] = 'a'; * $array['parent']['child'][1] = 'b'; * ...And so on. * _____________________________________ * @param simpleXMLElement $xml the XML to convert * @param boolean $flattenValues Choose wether to flatten values * or to set them under a particular index. * defaults to true; * @param boolean $flattenAttributes Choose wether to flatten attributes * or to set them under a particular index. * Defaults to true; * @param boolean $flattenChildren Choose wether to flatten children * or to set them under a particular index. * Defaults to true; * @param string $valueKey index for values, in case $flattenValues was set to * false. Defaults to "@value" * @param string $attributesKey index for attributes, in case $flattenAttributes was set to * false. Defaults to "@attributes" * @param string $childrenKey index for children, in case $flattenChildren was set to * false. Defaults to "@children" * @return array the resulting array. */ public static function simpleXMLToArray($xml, $flattenValues = true, $flattenAttributes = true, $flattenChildren = true, $valueKey = '@value', $attributesKey = '@attributes', $childrenKey = '@children') { $return = array(); if (!$xml instanceof SimpleXMLElement) { return $return; } $name = $xml->getName(); $_value = trim((string) $xml); if (strlen($_value) == 0) { $_value = null; } if ($_value !== null) { if (!$flattenValues) { $return[$valueKey] = $_value; } else { $return = $_value; } } $children = array(); $first = true; foreach ($xml->children() as $elementName => $child) { $value = self::simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey); if (isset($children[$elementName])) { if ($first) { $temp = $children[$elementName]; unset($children[$elementName]); $children[$elementName][] = $temp; $first = false; } $children[$elementName][] = $value; } else { $children[$elementName] = $value; } } if (count($children) > 0) { if (!$flattenChildren) { $return[$childrenKey] = $children; } else { $return = array_merge($return, $children); } } $attributes = array(); foreach ($xml->attributes() as $name => $value) { $attributes[$name] = trim($value); } if (count($attributes) > 0) { if (!$flattenAttributes) { $return[$attributesKey] = $attributes; } else { $return = array_merge($return, $attributes); } } if (count($return) == 0) { return ''; } return $return; }
function parse_math_tag($convoArr, simpleXMLElement $element, $parentName, $level) { $response = array(); $children = $element->children(); if (!empty($children)) { foreach ($children as $child) { $response[] = parseTemplateRecursive($convoArr, $child, $level + 1); } } else { $response[] = (string) $element; } $response_string = implode_recursive(' ', $response); // do something here runDebug(__FILE__, __FUNCTION__, __LINE__, "String to process: {$response_string}", 4); list($operator, $operand1, $operand2) = explode(' ', $response_string, 3); $operatorArray = array('ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE'); switch (true) { case !in_array(strtoupper($operator), $operatorArray): return 'Invalid math function!'; break; case !is_numeric($operand1): return "{$operand1} is not a number!"; break; case !is_numeric($operand2): return "{$operand2} is not a number!"; break; } switch ($operator) { case 'ADD': $out = $operand1 + $operand2; break; case 'SUBTRACT': $out = $operand1 - $operand2; break; case 'MULTIPLY': $out = $operand1 * $operand2; break; case 'DIVIDE': $out = $operand1 / $operand2; break; } return $out; }
//GET ALL THE FORM SUBMITTED VALUES $datafieldtag = $_POST['datafieldtag']; $ind1 = $_POST['ind1']; $ind2 = $_POST['ind2']; if (isset($_POST['_count_subfields-multiple_fields'])) { $count = $_POST['_count_subfields-multiple_fields']; } else { $count = 0; } foreach ($fields as $key => $val) { $xml_output = get_Datastream($val, $marcID); $xml_output_dc = get_Datastream($val, $dcID); $handle = retrieveHandle($xml_output_dc, $dictionary['HandleNamespace']); $xmlA = new simpleXMLElement($xml_output); $namespace = $xmlA->getNamespaces(true); $temp = $xmlA->children($namespace['default'])->addChild('datafield'); $temp->addAttribute('tag', $datafieldtag); $temp->addAttribute('ind1', $ind1); $temp->addAttribute('ind2', $ind2); $tempTest = $temp->addChild('subfield', $_POST['subfieldValue']); $tempTest->addAttribute('code', $_POST['subfieldCode']); for ($i = 1; $i <= $count; $i++) { $tempValue = "subfieldValue_" . $i; $tempCode = "subfieldCode_" . $i; $tempTest = $temp->addChild('subfield', $_POST[$tempValue]); $tempTest->addAttribute('code', $_POST[$tempCode]); } $marcTest = $xmlA->saveXML(); $marcxml = simplexml_load_string($marcTest); $agent = new Agent(); $dc_temp = $agent->transform2("./stylesheets/dc.xslt", $marcxml);
$STDOUT = fopen(dirname(__FILE__) . "/application.log", 'a+'); $STDERR = fopen(dirname(__FILE__) . "/../error.log", 'a+'); $keys = new keys(); $yesterday = date("Y-m-d", strtotime("yesterday")); $current_time = date("Y-m-d H:i:s", time()); printf("\n%s\n", $yesterday); $dbconnection = new mysqlconnect(); $dbconnection->connect($dbName); $mysqli = $dbconnection->connection; $merchantid = $keys::almerchantid; $authkey = $keys::alauthkey; $module = 'MerchantReport'; $reportid = '12'; $report = get_report($authkey, $merchantid, $module, $reportid, $yesterday); $xml = new simpleXMLElement($report); $rowcount = count($xml->children()); //print_r($xml->Table1[2]->Sales); $query = "insert into al_accountstats(accountdate, impressions, clicks, alsales, convertedsales, mobilesales, mobileconversions, commissions, incentives,\n\tnetworkcommissions, adj, newcustomers, newcustomersales, cpcfees, networkcpcfees, totalcommissions) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on duplicate key update \n\taccountdate=values(accountdate), impressions=values(impressions), clicks=values(clicks), alsales=values(alsales), convertedsales=values(convertedsales), mobilesales=values(mobilesales),\n\tmobileconversions=values(mobileconversions), commissions=values(commissions), incentives=values(incentives), networkcommissions=values(networkcommissions), adj=values(adj),\n\tnewcustomers=values(newcustomers), newcustomersales=values(newcustomersales), cpcfees=values(cpcfees), networkcpcfees=values(networkcpcfees), totalcommissions=values(totalcommissions)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("siidididddiidddd", $accountdate, $impressions, $clicks, $alsales, $convertedsales, $mobilesales, $mobileconversions, $commissions, $incentives, $networkcommissions, $adj, $newcustomers, $newscustomersales, $cpcfees, $networkcpcfees, $totalcommissions); $mysqli->query("start"); for ($i = 0; $i < $rowcount; ++$i) { $row = $xml->Table1[$i]; $date = strtotime($row->Date); $accountdate = date('Y-m-d', $date); $impressions = (int) str_replace(',', '', $row->Ad_Impressions); $clicks = (int) str_replace(',', '', $row->Click_Throughs); $alsales = floatval(str_replace(array(',', '$'), '', $row->Sales)); $convertedsales = $row->Number_of_Sales; $mobilesales = floatval(str_replace(array(',', '$'), '', $row->Mobile_Sales)); $mobileconversions = $row->Number_of_Mobile_Sales;
/** * Convert XML to array for bX recommendations * * @param \simpleXMLElement $xml XML to convert * @param string $ns Optional namespace for nodes * * @return array */ protected function convertToArray($xml, $ns = '') { $result = []; foreach ($xml->children($ns) as $node) { $children = $node->children($ns); if (count($children) > 0) { $item = $this->convertToArray($node, $ns); } else { $item = (string) $node; } $key = $node->getName(); if (isset($result[$key])) { if (!is_array($result[$key])) { $result[$key] = [$result[$key]]; } $result[$key][] = $item; } else { $result[$key] = $item; } } return $result; }