-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.php
167 lines (106 loc) · 3.25 KB
/
json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?
//https://github.com/Peekmo/JsonPath
require("jsonpath/JsonStore.php");
$input = json_decode(file_get_contents("data.js"),true); // results.titulo
$template = json_decode(file_get_contents("template.js"),true); //records.items
$store = new JsonStore();
$paths = [
[
"path" => "category[*].program[*]",
"child" => [
[
"key" => "nameprog",
"value" => "title"
],
//
]
],
[
"path" => "category[*].program[*].videos[*]",
"child" => [
[
"key" => "urls.app_iphone",
"value" => "data.iphone"
],
[
"key" => "urls.app_ipad",
"value" => "data.ipad"
]
]
]
];
function extract_data($record,$string) {
$current_data = $record;
foreach ($string as $name) {
if (key_exists($name, $current_data)) {
$current_data = $current_data[$name];
} else {
return null;
}
}
return $current_data;
}
function node($paths,$id,$j,$input,$_input,$template,$output)
{
// foreach ($paths as $path) {
$node = $paths[$id];
if($id > 0)
{
$apath = explode(".",$node["path"]);
$path = $apath[count($apath)-1];
}else
$path = $node["path"];
$store = new JsonStore();
$inputs = $store->get($input, "$.".$path);
$tpath = count($paths)-1;
if($id < $tpath)
$id++;
foreach ($inputs as $i => $record) {
if (!@array_key_exists($i, $output) and $j == 0)
$output[$i] = $template;
foreach ($node["child"] as $child) {
$akey = explode(".",$child["key"]);
$key = '["'.implode('"]["',$akey).'"]';
$return = extract_data($record,$akey);
$avalue = explode(".",$child["value"]);
// $value = '["'.implode('"]["',$avalue).'"]';
// if($id > 0)
$value = '["'.implode('"]["',$avalue).'"][]';
if(!empty($return))
{
//$isset = @extract_data($output[$i],$avalue);
if($j == 0)
eval("\$output[$i]$value = \"$return\";");
else
eval("\$output[$j]$value = \"$return\";");
}
}
$output = node($paths,$id,$i,$record,$inputs,$template,$output);
}
return $output;
}
$nodes = node($paths,0,0,$input,$input,$template,$output=[]);
// Recursively traverses a multi-dimensional array.
function fix_keys($array) {
$numberCheck = false;
foreach ($array as $k => $val) {
// echo count($val)."\n";
// print_r($val);
// echo "key =".$k."\n";
// echo "\n\n\n\n";
if(!is_numeric($k) and is_array($val) and array_key_exists(0, $val))
{
//$numberCheck = array("k" => $k,"v"=>$val);
//print_r($val);
$array[$k] = $val[0];
//return $array;
}
if (is_array($val) and count($val) > 1) $array[$k] = fix_keys($val); //recurse
}
return $array;
}
//echo json_encode($nodes);
//$fixed = fix_keys($nodes);
print_r($nodes);
//print_r($fixed);
//echo json_encode($fixed);