0

I have a problem to transform my hierarchical array like this:

array(
[0]=>
  array(3) {
    ["id"]=>
    int(2353011010)
    ["name"]=>
    string(17) "LEDER ACCESSOIRES"
    ["order"]=>
    int(15)
  }
[1]=>
  array(3) {
    ["id"]=>
    int(2371475010)
    ["name"]=>
    string(15) "SPORT AUFKLEBER"
    ["order"]=>
    int(25)
  }
[2]=>
  array(4) {
    ["id"]=>
    int(2563635010)
    ["name"]=>
    string(17) "KENNZEICHENHALTER"
    ["order"]=>
    int(10)
    ["children"]=>
    array(6) {
      [0]=>
      array(4) {
        ["id"]=>
        int(3854259010)
        ["name"]=>
        string(9) "EDELSTAHL"
        ["order"]=>
        int(92)
        ["children"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            int(20878056010)
            ["name"]=>
            string(5) "test1"
            ["order"]=>
            int(1)
          }
        }
      }
      [1]=>
      array(3) {
        ["id"]=>
        int(3854260010)
        ["name"]=>
        string(5) "CHROM"
        ["order"]=>
        int(91)
      }
    }
[3]=>
  array(4) {
    ["id"]=>
    int(19754330010)
    ["name"]=>
    string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
    ["order"]=>
    int(3)
    }
}
)

Into a flat ones like this:

array(
[0]=>
    array(3) {
        ["id"]=>
        int(2353011010)
        ["name"]=>
        string(17) "LEDER ACCESSOIRES"
        ["order"]=>
        int(15)
    }
[1]=>
    array(3) {
        ["id"]=>
        int(2371475010)
        ["name"]=>
        string(15) "SPORT AUFKLEBER"
        ["order"]=>
        int(25)
    }
[2]=>
    array(3) {
        ["id"]=>
        int(2563635010)
        ["name"]=>
        string(17) "KENNZEICHENHALTER"
        ["order"]=>
        int(10)
    }
[3]=>
    array(4) {
        ["id"]=>
        int(3854259010)
        ["name"]=>
        string(9) "EDELSTAHL"
        ["order"]=>
        int(92),
        ["parentId"]=> 2563635010
    }
[4]=>
    array(4) {
        ["id"]=>
        int(20878056010)
        ["name"]=>
        string(5) "test1"
        ["order"]=>
        int(1),
        ["parentId"]=> 2563635010
    }
[5]=>
    array(4) {
        ["id"]=>
        int(3854260010)
        ["name"]=>
        string(5) "CHROM"
        ["order"]=>
        int(91),
        ["parentId"]=> 2563635010
    }
[6]=>
  array(4) {
    ["id"]=>
    int(19754330010)
    ["name"]=>
    string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
    ["order"]=>
    int(3)
}
)

The children antities should be removed and every child element should get a parentId entity of the higher level id. I need this solution for transfering into DB.

thx

Joeker
  • 95
  • 1
  • 10
  • Can you share the JSON of your input array? – Sahil Gulati May 12 '17 at 11:02
  • You may check this: http://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array you'll find your answer there – Code Cooker May 12 '17 at 11:06
  • @SahilGulati The original array is too long, but here a part encoded into JSON: {"id":2371475010,"name":"SPORT AUFKLEBER","order":25},{"id":2563635010,"name":"KENNZEICHENHALTER","order":10,"children":[{"id":3854259010,"name":"EDELSTAHL","order":92,"children":[{"id":20878056010,"name":"test1","order":1},{"id":20878057010,"name":"test2","order":2}]},{"id":3854260010,"name":"CHROM","order":91},{"id":3854261010,"name":"CARBON","order":90}, – Joeker May 12 '17 at 11:37
  • @Smalldeveloper: Yes I have tried some solutions like that, but every result is different of my result-structure – Joeker May 12 '17 at 11:37
  • you should take a look at this post : http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array – St3an May 12 '17 at 12:10
  • @Stan: This post I found with Google too. But the solution with "RecursiveIteratorIterator" or "array_merge" doest work for me. Its remove all my parent key elements. – Joeker May 12 '17 at 12:24

1 Answers1

0

Now, I have create a "temporary" method that works for me, but is noch flexible to use:

function recursive($categories) {

    foreach ($categories as $value) {
        $result[$value->id]['id'] = $value->id;
        $result[$value->id]['name'] = $value->name;
        $result[$value->id]['order'] = $value->order;
        $result[$value->id]['parentId'] = 0;

        if(isset($value->children)) {
            $parentId = $value->id;

            foreach($value->children as $value2) {
                $result[$value2->id]['id'] = $value2->id;
                $result[$value2->id]['name'] = $value2->name;
                $result[$value2->id]['parentId'] = $parentId;

                if(isset($value2->children)) {
                    $parentId = $value2->id;

                    foreach($value2->children as $value3) {
                        $result[$value3->id]['id'] = $value3->id;
                        $result[$value3->id]['name'] = $value3->name;
                        $result[$value3->id]['parentId'] = $parentId;
                    }
                }
            }
        }
    }

    return $result;
}

Do anybody know a recursive solution for this method?

Joeker
  • 95
  • 1
  • 10