how to rewrite the function more compact

Viewed 64

I have a json of the form:

{
  "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": []
      }
    }
  }
}

I need to get a structured array from JSON, for that I have a function. What is the problem, I am confused by multiple checks and loops

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;
}

Example output:

"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" => []
                    ]

How can you rewrite this function to simplify it, get rid of a few checks and loops, so that it is more compact?

Thanks in advance for your answer!

2 Answers

You might prefer a basic recursive function to go through your source array($arr) and collect the result array($store) that is referenced by a key ($id).

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;
}

So, if you'd like the 'Plaintiff' array, you could do:

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

working demo

Currently your function limited to a certain depth. If you really want to write your own function, you would have to use a recursive function.

Anyway - there a better solutions. If you decoded the json by yourself, you can tell the decode function to return an array:

json_decode($yourJsonString, true);

You can also convert an Object (in this case probably a stdClass) to an array like this:

$array = (array)$object;
Related