我有数组对象,我想在PHP中使用此数组获得跟随输出

时间:2015-06-17 06:23:39

标签: php

category: { 
    1: {
      id: 1,
        category_id: 7,
        name: "N&E",
        parent: 0,
        created_at: "2015-06-17 01:36:03",
        …
    }
    2: {
      id: 2,
        category_id: 8,
        name: "Vauniya",
        parent: 1,
        created_at: "2015-06-17 01:36:03",
        …
    }
    3: {
        id: 3,
        category_id: 9,
        name: "MANNAR TOWN",
        parent: 2,
        created_at: "2015-06-17 01:36:03",
        …
    }
    4: {
        id: 4,
        category_id: 10,
        name: "KANDASAMY CHANDRAMURALI",
        parent: 3,
        created_at: "2015-06-17 01:36:04",
        …
    }
    5: {
        id: 5,
        category_id: 11,
        name: "",
        parent: 4,
        created_at: "2015-06-17 01:36:04",
        …
    }
    6: {
        id: 6,
        category_id: 12,
        name: "MATHUSHAN STORE",
        parent: 5,
        created_at: "2015-06-17 01:36:04",
        …
    }
    7: {
        id: 7,
        category_id: 13,
        name: "E.H PREMACHANDRAN",
        parent: 6,
        created_at: "2015-06-17 01:36:04",
        …
    }
    8: {
        id: 8,
        category_id: 14,
        name: "56870",
        parent: 7,
        created_at: "2015-06-17 01:36:04",
        …
    }
    9: {
        id: 9,
        category_id: 18,
        name: "E & D",
        parent: 7,
        created_at: "2015-06-17 01:36:04",
        …
    }
    10: {
        id: 10,
        category_id: 19,
        name: "PUNITHA PAVAN HOTEL",
        parent: 9,
        created_at: "2015-06-17 01:36:04",
        …
    }
}

我希望得到像这样的数组

mapArray(
 array(
 array(
      'children' => array(
         array(
           'children' => array(
              array(
               'children' => array(
                  array(
                   'children' => array()
                  ),
                  array(
                    'children' => array()
                  )
                )
              ),
              array(
                'children' => array()
              )
            )
          ),
          array(
            'children' => array()
          )
       )
     ),
     array(
       'children' => array()
     ),
     array(
       'children' => array()
     )
  )
);

1 个答案:

答案 0 :(得分:1)

Sriya,

这是你的意思吗?

Array
(
    [1] => Array
        (
            [category_id] => 7
            [name] => A
            [parent] => 0
            [created_at] => 2015-06-17 01:36:03
            [2] => Array
                (
                    [category_id] => 8
                    [name] => B
                    [parent] => 0
                    [created_at] => 2015-06-17 01:36:03
                    [3] => Array
                        (
                            [category_id] => 9
                            [name] => C
                            [parent] => 0
                            [created_at] => 2015-06-17 01:36:03
                            [4] => Array
                                (
                                    [category_id] => 10
                                    [name] => D
                                    [parent] => 0
                                    [created_at] => 2015-06-17 01:36:03
                                    [5] => Array
                                        (
                                            [category_id] => 11
                                            [name] => E
                                            [parent] => 0
                                            [created_at] => 2015-06-17 01:36:03
                                            [6] => Array
                                                (
                                                    [category_id] => 12
                                                    [name] => F
                                                    [parent] => 0
                                                    [created_at] => 2015-06-17 01:36:03
                                                    [7] => Array
                                                        (
                                                            [category_id] => 13
                                                            [name] => G
                                                            [parent] => 0
                                                            [created_at] => 2015-06-17 01:36:03
                                                            [8] => Array
                                                                (
                                                                    [category_id] => 14
                                                                    [name] => H
                                                                    [parent] => 0
                                                                    [created_at] => 2015-06-17 01:36:03
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我这样做,

> $category = array('1' => array('category_id'  => '7',
>                                           'name'          =>  'A',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '2'=>  array('category_id'  => '8',
>                                           'name'          =>  'B',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '3'=>  array('category_id'  => '9',
>                                           'name'          =>  'C',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '4'=>  array('category_id'  => '10',
>                                           'name'          =>  'D',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '5'=>  array('category_id'  => '11',
>                                           'name'          =>  'E',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '6'=>  array('category_id'  => '12',
>                                           'name'          =>  'F',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '7'=>  array('category_id'  => '13',
>                                           'name'          =>  'G',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03',
>                                           '8'=>  array('category_id'  => '14',
>                                           'name'          =>  'H',
>                                           'parent'        =>  '0',
>                                           'created_at'    =>  '2015-06-17 01:36:03'))))))))); print_r($category);