-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsonToPhp.php
97 lines (89 loc) · 2.12 KB
/
jsonToPhp.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
<?php
/*
Convert JSON to PHP Code
*/
########## Input JSON Here! ##########
$json = '{
"Description": "This is an example of an edited DL.",
"DisplayName": "ExampleDL",
"IsHiddenFromAddressList": false,
"EmailAddresses": [
{
"Action":"remove",
"Value": "exampledl-alias@example.com",
"AddressPrimary": false,
"AddressProtocol": "smtp"
}
],
"Members": {
"Recipients": [
{
"Action":"Remove",
"Value": 1
},
{
"Action":"Add",
"Value":"mexuser3"
}
]
},
"AcceptMessagesOnlyFrom": {
"All":"restricted",
"Recipients": [
{
"Action":"Remove",
"Value":"mexuser3"
},
{
"Action":"Add",
"Value":"mexuser2"
}
]
}
}
';
########## Input JSON Here! ##########
$data = json_decode($json, true);
function print_boolean($condition){
if($condition){
return "true";
} else {
return "false";
}
}
function is_multidimensional_array($a){
foreach($a as $v){
if(is_array($v)){
return true;
}
}
return false;
}
function process_json($data, $step){
foreach($data as $key => $value){
if(is_array($value)){
if(!is_multidimensional_array($value)){
echo str_repeat("\t", $step) . 'array (' . "\n";
process_json($value, $step + 1);
echo str_repeat("\t", $step) . ")," . "\n";
} else{
echo str_repeat("\t", $step) . '"' . $key . '" => array (' . "\n";
process_json($value, $step + 1);
echo str_repeat("\t", $step) . ")," . "\n";
}
}
else{
if(is_bool($value)){
echo str_repeat("\t", $step) . '"' . $key . '" => ' . print_boolean($value) . ',' . "\n";
} elseif(is_numeric($value)){
echo str_repeat("\t", $step) . '"' . $key . '" => ' . $value . ',' . "\n";
} else{
echo str_repeat("\t", $step) . '"' . $key . '" => "' . $value . '",' . "\n";
}
}
}
}
echo "\$value = array(\n";
process_json($data, 1);
echo ");\n";
?>