我有这个数组
Array
(
[condition] => AND
[rules] => Array
(
[0] => Array
(
[type] => query-builder-rule
[query] => Array
(
[id] => transactionDate
[field] => transactionDate
[type] => date
[input] => datepicker
[operator] => equal
[value] => 2017-05-02 00:00 - 2017-05-03 00:00
[rule] => transactionDate
)
)
[1] => Array
(
[type] => query-builder-group
[query] => Array
(
[condition] => OR
[rules] => Array
(
[0] => Array
(
[type] => query-builder-rule
[query] => Array
(
[id] => firstname
[field] => firstname
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => firstname
)
)
[1] => Array
(
[type] => query-builder-rule
[query] => Array
(
[id] => lastname
[field] => lastname
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => lastname
)
)
[2] => Array
(
[type] => query-builder-rule
[query] => Array
(
[id] => email
[field] => email
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => email
)
)
)
)
)
)
)
我需要将此转换为此数组
Array
(
[condition] => AND
[rules] => Array
(
[0] => Array
(
[id] => transactionDate
[field] => transactionDate
[type] => date
[input] => datepicker
[operator] => equal
[value] => 2017-05-02 00:00 - 2017-05-03 00:00
[rule] => transactionDate
)
[1] => Array
(
[condition] => OR
[rules] => Array
(
[0] => Array
(
[id] => firstname
[field] => firstname
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => firstname
)
[1] => Array
(
[id] => lastname
[field] => lastname
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => lastname
)
[2] => Array
(
[id] => email
[field] => email
[type] => string
[input] => text
[operator] => equal
[value] => Doe
[rule] => email
)
)
)
)
)
我试图以递归的方式做到这一点,但我碰到了墙。
我目前的代码是这样,但它并不是真的正确。
public function transform($request)
{
$request = json_decode($request, true);
foreach ($request as $key => $value) {
if ($key == 'rules') {
foreach ($value as $rKey => $rValue) {
if (!is_array($rValue['query']['rules'])) {
$this->parsedRequest[$key] = $rValue['query'];
} else {
$this->transform(json_encode($rValue['query']));
}
}
}
$this->parsedRequest[$key] = $value;
}
}
这只会覆盖现有值。我无法理解它。我尝试使用array_walk_recursive
,但我无法使用它。任何帮助表示赞赏。
P.S。我递归地需要这个,因为我不知道会有多少层。这些数组只是示例。
编辑:
var_export
第一个数组:
array ( 'condition' => 'AND', 'rules' => array ( 0 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'transactionDate', 'field' => 'transactionDate', 'type' => 'date', 'input' => 'datepicker', 'operator' => 'equal', 'value' => '2017-05-02 00:00 - 2017-05-03 00:00', 'rule' => 'transactionDate', ), ), 1 => array ( 'type' => 'query-builder-group', 'query' => array ( 'condition' => 'OR', 'rules' => array ( 0 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'firstname', 'field' => 'firstname', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'firstname', ), ), 1 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'lastname', 'field' => 'lastname', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'lastname', ), ), 2 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'email', 'field' => 'email', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'email', ), ), ), ), ), ), )
第二个数组:
array ( 'condition' => 'OR', 'rules' => array ( 0 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'firstname', 'field' => 'firstname', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'firstname', ), ), 1 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'lastname', 'field' => 'lastname', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'lastname', ), ), 2 => array ( 'type' => 'query-builder-rule', 'query' => array ( 'id' => 'email', 'field' => 'email', 'type' => 'string', 'input' => 'text', 'operator' => 'equal', 'value' => 'Doe', 'rule' => 'email', ), ), ), )
答案 0 :(得分:1)
在这里,我为你编写了代码。函数名称为simplify
,您的数组位于$arr
。
<?php
function simplify($arr) {
if(is_array($arr))
foreach($arr as $key => &$a) {
if($key == 'rules') {
if(is_array($a))
foreach($a as $key2 => &$a2) {
if(isset($a2['query'])) {
if(isset($a2['query']['rules'])) {
$a2['query'] = simplify($a2['query']);
}
$a2 = $a2['query'];
}
}
}
}
return $arr;
}
$arr = array(
'condition' => 'AND',
'rules' => array(
0 => array(
'type' => 'query-builder-rule',
'query' => array(
'id' => 'transactionDate',
'field' => 'transactionDate',
'type' => 'date',
'input' => 'datepicker',
'operator' => 'equal',
'value' => '2017-05-02 00:00 - 2017-05-03 00:00',
'rule' => 'transactionDate',
) ,
) ,
1 => array(
'type' => 'query-builder-group',
'query' => array(
'condition' => 'OR',
'rules' => array(
0 => array(
'type' => 'query-builder-rule',
'query' => array(
'id' => 'firstname',
'field' => 'firstname',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'Doe',
'rule' => 'firstname',
) ,
) ,
1 => array(
'type' => 'query-builder-rule',
'query' => array(
'id' => 'lastname',
'field' => 'lastname',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'Doe',
'rule' => 'lastname',
) ,
) ,
2 => array(
'type' => 'query-builder-rule',
'query' => array(
'id' => 'email',
'field' => 'email',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'Doe',
'rule' => 'email',
) ,
) ,
) ,
) ,
) ,
) ,
);
$arr = simplify($arr);