【问题标题】:Laravel 5.5: Convert multidimensional array to single oneLaravel 5.5:将多维数组转换为单维数组
【发布时间】:2018-05-03 09:27:13
【问题描述】:

我想将以下多维数组转换为单个数组。 子数组的parent_id 列应该与父数组的category_id 相同。而子数组的sort_order_number 应该等于它们在children 数组中的位置。并且父数组的sort_order_number 应该等于它们在 parent 数组中的位置:

当前多维数组

array:2 [▼
  0 => array:12 [▼
    "text_az" => "title1"
    "text_en" => "title1.1"
    "text_ru" => "title1.2"
    "slug" => "sfsf"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 1
    "sort_order_number" => 1
    "parent_id" => null
    "created_at" => null
    "updated_at" => null
    "children" => array:1 [▼
      0 => array:12 [▼
        "text_az" => "title3"
        "text_en" => "title3.1"
        "text_ru" => "title3.2"
        "slug" => "sdf"
        "icon" => "fa fa-bell"
        "target" => "_top"
        "id" => 3
        "sort_order_number" => 1
        "parent_id" => null
        "created_at" => null
        "updated_at" => null
        "children" => array:1 [▼
          0 => array:11 [▼
            "text_az" => "title2"
            "text_en" => "title2.1"
            "text_ru" => "title2.2"
            "slug" => "fsdfsf"
            "icon" => "fa fa-bell"
            "target" => "_top"
            "id" => 2
            "sort_order_number" => 2
            "parent_id" => null
            "created_at" => null
            "updated_at" => null
          ]
        ]
      ]
    ]
  ]
  1 => array:11 [▼
    "text_az" => "title4"
    "text_en" => "title4.1"
    "text_ru" => "title4.2"
    "slug" => "treteterter"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 4
    "sort_order_number" => 2
    "parent_id" => null
    "created_at" => null
    "updated_at" => null
  ]
]

转换后我希望拥有的一维数组:

array:3 [▼
  0 => array:12 [▼
    "text_az" => "title1"
    "text_en" => "title1.1"
    "text_ru" => "title1.2"
    "slug" => "sfsf"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 1
    "sort_order_number" => 1
    "parent_id" => null
    "created_at" => null
    "updated_at" => null
  ]
  1 => array:12 [▼
    "text_az" => "title3"
    "text_en" => "title3.1"
    "text_ru" => "title3.2"
    "slug" => "sdf"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 3
    "sort_order_number" => 1
    "parent_id" => 1
    "created_at" => null
    "updated_at" => null

  ]
  2 => array:12 [▼
    "text_az" => "title2"
    "text_en" => "title2.1"
    "text_ru" => "title2.2"
    "slug" => "fsdfsf"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 2
    "sort_order_number" => 2
    "parent_id" => 3
    "created_at" => null
    "updated_at" => null

  ]
  3 => array:12 [▼
    "text_az" => "title4"
    "text_en" => "title4.1"
    "text_ru" => "title4.2"
    "slug" => "treteterter"
    "icon" => "fa fa-bell"
    "target" => "_top"
    "id" => 4
    "sort_order_number" => 2
    "parent_id" => null
    "created_at" => null
    "updated_at" => null

  ]

]

【问题讨论】:

  • 我假设您尝试了一些方法来提出您想要的结构,请在此处发布代码
  • 使用递归,检查当前key是否为数组,如果是则添加到新数组中。
  • 您能根据我的解释告诉我如何做到这一点吗?我需要 php 中的代码示例。我尝试了很多方法,但没有一个对我有用。 (

标签: php arrays laravel multidimensional-array laravel-5.5


【解决方案1】:

由于你的数组是一个固定的结构,你可以这样做,一个 simple demo

$result = [];
$count = 1;
$temp = [];
array_walk_recursive($array, function($v, $k) use(&$result, &$count, &$temp) {
    $temp[$k] = $v;
    if($count++ % 11 == 0) {
        $temp["sort_order_number"] = intval($count / 11);
        $result[] = $temp;
        $temp = [];
    }
});

【讨论】:

  • 它只给了我空数组结果(
  • 我用&$result而不是$result编辑了我的答案
  • 感谢克里斯,它成功了。但是 sort_order_number 呢?我该如何解决?
  • 克里斯,对不起。上一期:如何解决parent_id的问题?
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 2019-06-29
  • 2011-10-10
  • 1970-01-01
相关资源
最近更新 更多