如何更紧凑地重写函数

时间:2021-06-08 11:26:25

标签: php arrays json

我有一个 json 格式:

{
  "result": {
    "Plaintiff": {
      "2015": {
        "SolutionsPerv": {
          "To leave the decision (determination) of the court of first instance and the ruling of the court of appeal unchanged, and the cassation appeal - unsatisfied": {
            "Amount": 0,
            "Quantity": 1
          }
        },
        "DecisionsApp": {
          "To leave the decision (determination) of the court of first instance and the ruling of the court of appeal unchanged, and the cassation appeal - unsatisfied": {
            "Amount": 0,
            "Quantity": 1
          }
        },
        "ResheniyaKass": {
          "To leave the decision (determination) of the court of first instance and the ruling of the court of appeal unchanged, and the cassation appeal - unsatisfied": {
            "Amount": 0,
            "Quantity": 1
          }
        },
        "ResheniyaNadz": []
      }
    },
    "Respondent": {
      "2018": {
        "SolutionsPerv": {
          "Leave the ruling of the court of first instance and the ruling of the court of appeal unchanged, the cassation appeal without satisfaction": {
            "Amount": 24000,
            "Quantity": 1
          }
        },
        "ResheniyaKass": {
          "Leave the ruling of the court of first instance and the ruling of the court of appeal unchanged, the cassation appeal without satisfaction": {
            "Amount": 24000,
            "Quantity": 1
          }
        },
        "ResheniyaNadz": []
      },
      "2019": {
        "ResheniyaKass": {
          "To leave the decision (determination) of the court of first instance and the ruling of the court of appeal unchanged, and the cassation appeal - unsatisfied": {
            "Amount": 0,
            "Quantity": 1
          }
        },
        "ResheniyaNadz": []
      },
      "2020": {
        "SolutionsPerv": {
          "There is no decision": {
            "Amount": 0,
            "Quantity": 2
          }
        },
        "DecisionsApp": [],
      }
    },
    "Third Person": {
      "2015": {
        "SolutionsPerv": {
          "To leave unchanged the decision and (or) the decision of the appellate instance, and the cassation appeal - without satisfaction (clause 1 of part 1 of article 287 of the APC)": {
            "Amount": 0,
            "Quantity": 1
          }
        },
        "ResheniyaNadz": []
      }
    }
  }
}

我需要从 JSON 获取结构化数组,为此我有一个函数。有什么问题,我被多次检查和循环弄糊涂了

private function getArray(array $items): array
{
    $data = [];
    foreach ($items as $key => $value) {
        if (is_array($value)) {
            array_push($data, $key);
            foreach ($value as $items => $item) {
                if (is_array($value)) {
                    array_push($data, $items, $item);
                }
            }
        }
    }

    return $data;
}

示例输出:

"Defendant",
                    2017,
                    [
                        "SolutionsPerv" => [
                            "To leave the decision unchanged, and the appeal - without satisfaction (paragraph 1 of article 269 of the APC)" => [
                                "Amount" => 10576596.8,
                                "Quantity" => 1
                            ],
                            "Leave the decision (ruling) of the first instance court and the decision of the court of appeal unchanged, and the cassation appeal without satisfaction" => [
                                "Amount" => 3519672.72,
                                "Quantity" => 1
                            ]
                        ],
                        "DecisionsApp" => [
                            "To leave the decision unchanged, and the appeal - without satisfaction (paragraph 1 of article 269 of the APC)" => [
                                "Amount" => 10576596.8,
                                "Quantity" => 1
                            ],
                            "Leave the decision (ruling) of the first instance court and the decision of the court of appeal unchanged, and the cassation appeal without satisfaction" => [
                                "Amount" => 3519672.72,
                                "Quantity" => 1
                            ]
                        ],
                        "ResheniyaKass" => [
                            "Leave the decision (ruling) of the first instance court and the decision of the court of appeal unchanged, and the cassation appeal without satisfaction" => [
                                "Amount" => 3519672.72,
                                "Quantity" => 1
                            ]
                        ],
                        "DecisionsNadz" => [

                        ]
                    ],
                    2021,
                    [
                        "SolutionsPerv" => [
                            "No solution" => [
                                "Amount" => 44014383.56,
                                "Quantity" => 1
                            ]
                        ],
                        "DecisionsApp" => [],
                        "ResheniyaKass" => [],
                        "DecisionsNadz" => []
                    ]

你如何重写这个函数来简化它,去掉一些检查和循环,使它更紧凑?

预先感谢您的回答!

2 个答案:

答案 0 :(得分:1)

您可能更喜欢使用基本递归函数来遍历源数组 ($arr) 并收集键 ($store) 引用的结果数组 ($id)。< /p>

function getArray(array $arr = [], string $id = '', &$store = []): array
{
    foreach ($arr as $key => $value) {
        if ($key === $id && is_array($value)) { // identifier found
            return $store = $value; // store one result array
            // $store[] = $value; // store multiple result arrays
        } elseif (is_array($value)) getArray($value, $id, $store); // keep digging
    }
    return $store;
}

因此,如果您想要“原告”数组,您可以这样做:

$result = getArray($arr, 'Plaintiff');

工作demo

答案 1 :(得分:0)

目前您的功能仅限于一定深度。如果您真的想编写自己的函数,则必须使用递归函数。

无论如何 - 有更好的解决方案。如果自己解码json,可以告诉decode函数返回一个数组:

#!/bin/sh -f

您还可以将对象(在本例中可能是 stdClass)转换为这样的数组:

json_decode($yourJsonString, true);