<?php
$stack = array();

function startTag($parser, $name, $attrs) {
	global $stack;
	$tag=array("name"=>$name,"attrs"=>$attrs);
	array_push($stack,$tag);
}

function cdata($parser, $cdata) {
	global $stack,$i;
	if(trim($cdata)) {
		$stack[count($stack)-1]['cdata']=$cdata;
	}
}

function endTag($parser, $name) {
	global $stack;
	$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
	array_pop($stack);
}

function xml2array($xml_data) {
	global $stack;
	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startTag", "endTag");
	xml_set_character_data_handler($xml_parser, "cdata");

	$data = xml_parse($xml_parser,$xml_data);
	if(!$data) {
		die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)));
	}

	xml_parser_free($xml_parser);
	return $stack;
}

$filename = 'local.xml';
$slabs = xml2array(file_get_contents($filename));
$provider_index=-1;
$country_index=-1;
$cost_index = 0;
$name_xml = '';
//echo "var SERVICE = ".SERVICE.";";
# check if there is any countries
if (isset($slabs[0]['children']) && is_array($slabs[0]['children'])) 
	{
	# countries one by one
	foreach ($slabs[0]['children'] as $i=>$slabb) 
		{
		$slab = $slabb['attrs'];
		if($slab['COUNTRY']=='RU') 
			{
			if($name_xml != $slab['COUNTRY_NAME']) 
				{
				$cost_index = 0;
				$country_index++;
				}
			if (isset($slabb['children']) && count($slabb['children']) > 0) 
				{
				foreach ($slabb['children'] as $k=>$slab_provider) 
					{
					$slab = $slab_provider['attrs'];
					if($name_xml != $slab['NAME']) 
						{
						$provider_index++;
						$cost_index=0;
						$arr_xml[$provider_index] = $slab['NAME'];
						}
					$arrk_xml[$provider_index]['rub'][$cost_index]=$slab['PRICE'];
					$arrk_xml[$provider_index]['usd'][$cost_index]=$slab['USD'];
					$name_xml = $slab['NAME'];
					$cost_index++;
					}
				$provider_index = -1;
				}
			}
		} 
	}

$i=0;
if($arr_xml)
	{
	echo '<table style="display:none" id="tab_sms" width=100%>';
	foreach($arr_xml as $index_xml=>$ar_xml)
		{
		if(!$i) 
		echo '<tr>';
		echo '<td>';
		echo $ar_xml;
		sort($arrk_xml[$index_xml]['usd']);
		sort($arrk_xml[$index_xml]['rub']);
		foreach ($arrk_xml[$index_xml]['usd'] as $ind_xml => $a_xml)
			{
			if($a_xml>=$cena_xml)
				{
				echo ' - <font color=red>'.$arrk_xml[$index_xml]['rub'][$ind_xml].'</font>'; break;
				}
			}
		$i++; if($i==3) $i=0;
		}
	echo '</table>';
	}
?>
