Skip to content

Instantly share code, notes, and snippets.

@Grom-S
Created October 14, 2013 07:55
Show Gist options
  • Save Grom-S/6972289 to your computer and use it in GitHub Desktop.
Save Grom-S/6972289 to your computer and use it in GitHub Desktop.
<?php
$products = array(
'Media Player',
'Video Converter'
);
$file_data = file_get_contents("template.json");
$main_json = json_decode($file_data);
$result = array();
foreach ($products as $product) {
$result[] = replaceProductName($main_json[0], $product);
}
$json_data = json_encode($result, JSON_PRETTY_PRINT);
file_put_contents("products.json", $json_data);
function replaceProductName($json, $product)
{
// force copy of the object
$json = unserialize(serialize($json));
foreach ($json as $item_id => $item) {
if (strpos($item_id, "product_name") === false) {
continue;
}
$item->text = $product;
}
return $json;
}
[
{
"slide-1": {
"width": "300",
"height": "250",
"backgroundColor": "#f2f2f2",
"border": {
"thickness": 0,
"color": "#000000",
"radius": 0
}
},
"image-2": {
"top": "110px",
"left": "90px",
"width": "auto",
"height": "auto",
"zIndex": "",
"src": "/bundles/rstmain/images/image-placeholder.gif"
},
"product_name": {
"top": "30px",
"left": "45px",
"width": "auto",
"height": "auto",
"zIndex": "",
"maxFontSize": "14",
"font": "arial",
"text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. ",
"color": "#333"
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment