4

次の配列があります。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配列の結果です。id isと servicesparentId Servicesis の下に is is が表示されます。About22

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_walkor array_mapand でforeach簡単にできます。

functionSQL JOIN なしで配列インデックスを結合するものがあるのだろうforeach loopか?

だから私の配列で:id = parentId

4

2 に答える 2

1

このライブラリを試してください

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',
   )),
 )
于 2017-10-26T09:48:37.690 に答える