4

I have following array. parentId key important!

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] => 
        )

)

And below is my expected array result. You'll see the Services is is under the About that have id is 2 and services parentId is 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] => 
                        )

                )

        )

)

I can do this with array_walk or array_map and foreach easily.

I just wonder that is there any function that join array indexes like SQL JOIN without foreach loop?

So in my array: id = parentId

Bora
  • 9,846
  • 4
  • 39
  • 66
  • Perhaps using [`array_merge_recursive`](http://www.php.net/manual/en/function.array-walk-recursive.php) using a user-defined callback? – Elias Van Ootegem Sep 20 '13 at 11:48
  • @EliasVanOotegem that would be the same as with array_map, I think – Alma Do Sep 20 '13 at 11:49
  • @AlmaDoMundo: Not quite, since it's not recursive... but you'll have t use `array_map($array, 'array_merge_recursive', $someParam)` – Elias Van Ootegem Sep 20 '13 at 11:51
  • @EliasVanOotegem I mean that you'll _iterate_ through array with that in PHP (while OP want to avoid that). Well, almost every array function iterates through input array (simply in it's C-implementation - i.e. it's hided from user) - so I'm not sure why it's so important for OP. – Alma Do Sep 20 '13 at 11:54
  • @AlmaDoMundo: Well, if OP doesn't want to loop, he shouldn't be programming :-P [I've seen another question like this](http://stackoverflow.com/questions/18262551/can-the-for-loop-be-eliminated-from-this-piece-of-php-code/18301948#18301948), basically: loops are inevitable... (that comment is meant for the OP, obviously) – Elias Van Ootegem Sep 20 '13 at 11:56
  • @EliasVanOotegem yes. This is a part of [Structured program theorem](http://en.wikipedia.org/wiki/Structured_program_theorem) - which points that every program could be written with consecutive loops and conditional operators. – Alma Do Sep 20 '13 at 11:58

2 Answers2

1

try this library

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

DATA

$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"],
];

USAGE

$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();

OUTPUT

 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',
   )),
 )
0

array the array like how it's done in images.

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

I personally would do it with two tables, then use one to reference the other based upon the key.

a coder
  • 545
  • 4
  • 23