Skip to content

Instantly share code, notes, and snippets.

@janisdonis
Created April 26, 2017 09:35
Show Gist options
  • Save janisdonis/c224d2d4c4b4552a0c215878d8c2b558 to your computer and use it in GitHub Desktop.
Save janisdonis/c224d2d4c4b4552a0c215878d8c2b558 to your computer and use it in GitHub Desktop.
<?php
$url = 'http://redeemnordics.com/pricelist/JSON/json.php';
$json = file_get_contents($url);
$array = json_decode($json);
$array = (array) $array;
$products = array();
$xml = new SimpleXMLElement('<products/>');
foreach ($array['data'] as $key => $product) {
$cats = explode(' ', $product->Makemodel);
$product->manucacter = '';
if (isset($cats[0]) && !empty([$cats[0]])) {
$product->manucacter = $cats[0];
}
$product->sub_cat = '';
if (isset($cats[1]) && !empty([$cats[1]])) {
$product->sub_cat = $cats[1];
if (isset($cats[2]) && !empty([$cats[2]])) {
$product->sub_cat = $product->sub_cat . ' ' . $cats[2];
}
}
$grade = str_replace('Grade ', '', $product->Condition);
$new_grade = conditionMap($grade);
$product->Condition = $new_grade;
$child = $xml->addChild('product');
foreach ($product as $key => $value) {
if ($key == 'Makemodel') {
$key = 'title';
}
$child->addChild($key, htmlspecialchars($value));
}
$products[] = (array) $product;
}
echo $xml->saveXML();
function conditionMap($value) {
$orginal = array(
'Brand new',
'Swap',
'A+',
'A',
'B',
'C+',
'C',
);
$renew = array(
'Jauns',
'Jauns bez kastes',
'A+',
'A',
'B',
'B',
'C',
);
$new_value_key = array_search($value, $orginal);
$new_value = $renew[$new_value_key];
return $new_value;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment