简体   繁体   English

PHP返回带有已修改数据的JSON

[英]PHP Returning JSON with modified data

I want my categories list be nice formatted while return to user. 我希望我的类别列表在返回用户时能保持良好的格式。 What I get from database is: 我从数据库中得到的是:

[
{
    "id": 1,
    "name": "pet",
    "parent_id": null
},
{
    "id": 2,
    "name": "page",
    "parent_id": null
},
{
    "id": 3,
    "name": "dog",
    "parent_id": 1
},
{
    "id": 4,
    "name": "cat",
    "parent_id": 1
},
{
    "id": 5,
    "name": "rodent",
    "parent_id": 1
},...

I want it to keep tree structure, like: 我希望它保持树状结构,例如:

{
    "id": 1,
    "name": "pet",
    "parent_id": null,
    "children": [
        {
            "id": 3,
            "name": "dog",
            "parent_id": 1
        },
        {
            "id": 4,
            "name": "cat",
            "parent_id": 1
        },...

etc. 等等

Is there an easy way way to do it or I have to loop through the database results and create new organized array to return? 有没有简单的方法可以做到这一点,或者我必须遍历数据库结果并创建新的有组织的数组以返回? What is the best approach to do that? 最好的方法是什么? The problem is that the subcategories could also have subcategories. 问题在于子类别也可以具有子类别。 Or maybe i should just keep structure I get from the database and add children id's as an array (as I can refer to them anyway)? 或者,也许我应该保持从数据库中获取的结构并将子代ID添加为数组(因为无论如何我都可以引用它们)?

I would be grateful for your help. 谢谢您的帮助。

Thank you. 谢谢。

Solution: 解:

function normalize_db_animals(){

    $values[] = ["id" => 1, "name" => "pet", "parent_id" => null];
    $values[] = ["id" => 2, "name" => "dog", "parent_id" => 1];
    $values[] = ["id" => 3, "name" => "cat", "parent_id" => 1];
    $values[] = ["id" => 4, "name" => "rodent", "parent_id" => 1];

    $values[] = ["id" => 5, "name" => "wild", "parent_id" => null];
    $values[] = ["id" => 6, "name" => "tiger", "parent_id" => 5];
    $values[] = ["id" => 7, "name" => "rhino", "parent_id" => 5];

    $normalize = function () use ($values) {
        $tree = [];
        $i = 0;
        do {
            $pet = $values[$i];
            if ($pet['parent_id']) {
                if (array_key_exists($pet['parent_id'], $tree)) {
                    $tree[$pet['parent_id']]['children'][] = $pet;
                }
            } else {
                $tree[$pet['id']] = $pet;
            }

            $i++;
        } while ($i < count($values));

        return $tree;
    };

    $tree = $normalize();
    echo json_encode($tree);
}

Result: 结果:

   {"1":{"id":1,"name":"pet","parent_id":null,"children":[{"id":2,"name":"dog","parent_id":1},{"id":3,"name":"cat","parent_id":1},{"id":4,"name":"rodent","parent_id":1}]},"5":{"id":5,"name":"wild","parent_id":null,"children":[{"id":6,"name":"tiger","parent_id":5},{"id":7,"name":"rhino","parent_id":5}]}}

Try this one 试试这个

$a = json_decode('[{
      "id": 1,
      "name": "pet",
      "parent_id": null
    },
    {
      "id": 2,
      "name": "page",
      "parent_id": null
    },
    {
      "id": 3,
      "name": "dog",
      "parent_id": 1
    },
    {
      "id": 4,
      "name": "cat",
      "parent_id": 1
    },
    {
      "id": 5,
      "name": "rodent",
      "parent_id": 4
    },
    {
      "id": 6,
      "name": "rodent",
      "parent_id": 2
    }]');

    $a = collect($a);

    $filtered = $a;

    foreach ($filtered as $key => $value) {
      $children = $a->where('parent_id', $value->id);
      if(!$children->isEmpty()){
        $value->children = $children;
       $filtered->forget(array_values(array_keys($children->toArray())));

      }

    }
    dd($filtered);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM