php 基于id-parentId关系将二维数组转换为分层多维数组

km0tfn4u  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(103)

我有以下数组。parentId关键重要!

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Home
            [parentId] => 
            [children] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => About
            [parentId] => 
            [children] => 
        )

    [2] => Array
        (
            [id] => 3
            [name] => Services
            [parentId] => 2
            [children] => 
        )

)

下面是我的expected数组结果。您将看到Services位于About之下,其id为2,服务parentId为2

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Home
            [parentId] => 
            [children] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => About
            [parentId] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => Services
                            [parentId] => 2
                            [children] => 
                        )

                )

        )

)

我可以很容易地用array_walkarray_mapforeach做到这一点。
我只是想知道是否有任何function连接数组索引,如SQL JOIN没有foreach loop
在我的数组中:id = parentId

2uluyalo

2uluyalo1#

试试这个图书馆

https://github.com/erdalceylan/array-join
资料

$users = [
    ["id"=>1, "nick"=>"erdal"],
     (object)["id"=>2, "nick"=>"furkan" ],
    ["id"=>3, "nick"=>"huseyin"],
    ["id"=>4, "nick"=>"hümeyra" ],
    ["id"=>5, "nick"=>"tuba" ],
];

 $items = [
     ["user_id"=>1, "item"=>"kaban", "mmx" => "mmx1"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx2"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx3"],
     (object)["user_id"=>1, "item"=>"çorap", "mmx" => "mmx4"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx5"],
    ["user_id"=>1, "item"=>"çorap", "mmx" => "mmx6"],
    ["user_id"=>2, "item"=>"araba", "mmx" => "mmx7"],
     (object)["user_id"=>9, "item"=>"ev", "mmx" => "mmx8"],
    ["user_id"=>10, "item"=>"yat", "mmx" => "mmx9"],
];

$foods = [
    ["user_id"=>1, "food"=>"iskender"],
    ["user_id"=>2, "food"=>"adana"],
];

$texts = [
    ["user_id"=>1, "text"=>"merhaba"],
    ["user_id"=>15, "text"=>" hi"],
];

用途

$instance = \ArrayJoin\Builder::newInstance()
    ->select("a.id", "a.nick", "b.item", "d.food")
    ->from($users, "a")
    ->innerJoin($items, "b", new \ArrayJoin\On("a.id = b.user_id"))
    ->leftJoin($texts, "c", new \ArrayJoin\On("a.id = c.user_id"))
    ->rightJoin($foods, "d", new \ArrayJoin\On("b.user_id = d.user_id"))
     ->where("a.id", "a.text", function ($fieldFirs, $fieldSecond){
         return $fieldFirs < 10;
     })
     ->limit(2)
     ->offset(1)
     ->setFetchType(\ArrayJoin\Builder::FETCH_TYPE_OBJECT);

 $instance->execute();

输出

array (
   stdClass::__set_state(array(
      'id' => 1,
      'nick' => 'erdal',
      'item' => 'çorap',
      'food' => 'iskender',
   )),
   stdClass::__set_state(array(
      'id' => 1,
      'nick' => 'erdal',
      'item' => 'çorap',
      'food' => 'iskender',
   )),
 )
iih3973s

iih3973s2#

数组数组,就像在图像中一样。

[1][children][0] => Array
                    (
                        [id] => 3
                        [name] => Services
                        [parentId] => 2
                        [children] => 
                    )

我个人会用两个表来做,然后使用一个来引用另一个基于密钥。

相关问题