如何将 JSON 数据转换为没有键的数组?
How do you convert JSON data to array without keys?
我有一组 JSON 规则,我正试图将其转换为 PHP 数组。
这是规则集:
{
"rules" : {
"full_name" : "required",
"email" : {
"required" : true,
"email" : true
},
"phone" : {
"required" : true,
"maxlength" : 10
},
"comments" : {
"minlength" : 10
}
}
}
这是我正在努力的最终输出:
$rules = [
'full_name' => 'required',
'email' => [ 'required', 'email'],
'phone' => [ 'max_length' => 10 ],
'message' => [ 'min_length' => 10 ]
];
我创建了这个翻译方法,它读取 json_decode 之后的 json 数据:
private function convertRules($rules)
{
$convertedRules = [];
foreach( $rules as $key => $value ) {
if( is_array($value)) {
$value = $this->convertRules($value);
}
switch( $key ) {
case '1':
$value = $key;
break;
case 'minlength':
$key = 'min_length';
break;
case 'maxlength':
$key = 'max_length';
break;
}
switch( $value ) {
case '1':
$value = $key;
break;
}
$convertedRules[$key] = $value;
}
return $convertedRules;
}
但是它创建了一个数组,其中的键与值重复。
Array
(
[full_name] => required
[email] => Array
(
[required] => required
[email] => email
)
[phone] => Array
(
[required] => required
[max_length] => 10
)
[comments] => Array
(
[min_length] => 10
)
)
如何获得类似于上面 PHP 规则数组的没有键的数组?
任何 PHP7 优化也表示赞赏!
您可以像这样调整 convert_rules
函数来产生所需的结果:
function convert_rules($rules) {
$result = [];
foreach ($rules as $name => $ruleSet) {
if (!is_array($ruleSet)) {
$result[$name] = $ruleSet;
} else {
$result[$name] = [];
foreach ($ruleSet as $rule => $value) {
switch ($rule) {
case 'maxlength':
$result[$name]['max_length'] = $value;
break;
case 'minlength':
$result[$name]['min_length'] = $value;
break;
default:
$result[$name][] = $rule;
}
}
}
}
return $result;
}
如您所见,默认行为是插入没有键的规则。对于其他任何事情,创建一个单独的 case
.
就足够了
我有一组 JSON 规则,我正试图将其转换为 PHP 数组。
这是规则集:
{
"rules" : {
"full_name" : "required",
"email" : {
"required" : true,
"email" : true
},
"phone" : {
"required" : true,
"maxlength" : 10
},
"comments" : {
"minlength" : 10
}
}
}
这是我正在努力的最终输出:
$rules = [
'full_name' => 'required',
'email' => [ 'required', 'email'],
'phone' => [ 'max_length' => 10 ],
'message' => [ 'min_length' => 10 ]
];
我创建了这个翻译方法,它读取 json_decode 之后的 json 数据:
private function convertRules($rules)
{
$convertedRules = [];
foreach( $rules as $key => $value ) {
if( is_array($value)) {
$value = $this->convertRules($value);
}
switch( $key ) {
case '1':
$value = $key;
break;
case 'minlength':
$key = 'min_length';
break;
case 'maxlength':
$key = 'max_length';
break;
}
switch( $value ) {
case '1':
$value = $key;
break;
}
$convertedRules[$key] = $value;
}
return $convertedRules;
}
但是它创建了一个数组,其中的键与值重复。
Array
(
[full_name] => required
[email] => Array
(
[required] => required
[email] => email
)
[phone] => Array
(
[required] => required
[max_length] => 10
)
[comments] => Array
(
[min_length] => 10
)
)
如何获得类似于上面 PHP 规则数组的没有键的数组?
任何 PHP7 优化也表示赞赏!
您可以像这样调整 convert_rules
函数来产生所需的结果:
function convert_rules($rules) {
$result = [];
foreach ($rules as $name => $ruleSet) {
if (!is_array($ruleSet)) {
$result[$name] = $ruleSet;
} else {
$result[$name] = [];
foreach ($ruleSet as $rule => $value) {
switch ($rule) {
case 'maxlength':
$result[$name]['max_length'] = $value;
break;
case 'minlength':
$result[$name]['min_length'] = $value;
break;
default:
$result[$name][] = $rule;
}
}
}
}
return $result;
}
如您所见,默认行为是插入没有键的规则。对于其他任何事情,创建一个单独的 case
.