2017-07-16 11 views
0

私は親と子で構成されるカテゴリ配列を持っています。誰かがforeachループでparent_idとcategory_idを使用して、配列の配列を整理する手助けをすることができます。一番上のParentのparent_idは「0」です。 category_idとparent_id以外のフィールドは無視してください。ここに私の配列です。親子関係の配列

Array 
(
    [categories] => Array 
     (
      [0] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1527 
        [category_name] => CABIN 
        [category_description] => a seperate cabin 
        [capacity] => 100 
        [category_price] => 50000 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [1] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1528 
        [category_name] => C3 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1527 
        [is_parent] => 0 
       ) 

      [2] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1529 
        [category_name] => FLOOR3 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1528 
        [is_parent] => 0 
       ) 

      [3] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1530 
        [category_name] => CABIN 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [4] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1531 
        [category_name] => C3 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1530 
        [is_parent] => 0 
       ) 

      [5] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1532 
        [category_name] => FLOOR1 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1531 
        [is_parent] => 0 
       ) 

      [6] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1533 
        [category_name] => CABIN 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [7] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1534 
        [category_name] => C5 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1533 
        [is_parent] => 0 
       ) 

      [8] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1535 
        [category_name] => FLOOR1 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1534 
        [is_parent] => 0 
       ) 

      [9] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1536 
        [category_name] => CABIN 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [10] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1537 
        [category_name] => C3 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1536 
        [is_parent] => 0 
       ) 

      [11] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1538 
        [category_name] => FLOOR2 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1537 
        [is_parent] => 0 
       ) 

      [12] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1539 
        [category_name] => CABIN 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [13] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1540 
        [category_name] => ABOVE SBI 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1539 
        [is_parent] => 0 
       ) 

      [14] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1541 
        [category_name] => FLOOR2 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1540 
        [is_parent] => 0 
       ) 

      [15] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1542 
        [category_name] => Launch Pad 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 0 
        [is_parent] => 0 
       ) 

      [16] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1543 
        [category_name] => C5 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1542 
        [is_parent] => 0 
       ) 

      [17] => Array 
       (
        [workspace_id] => 1 
        [category_id] => 1544 
        [category_name] => FLOOR3 
        [category_description] => 
        [capacity] => 
        [category_price] => 
        [status] => 1 
        [created_by] => 1 
        [created_time] => 2017-07-14 07:56:54 
        [updated_by] => 1 
        [updated_time] => 2017-07-14 07:56:54 
        [label_id] => 1 
        [parent_id] => 1543 
        [is_parent] => 0 
       ) 

     ) 

) 
+1

をチャイルズ? –

+0

あなたが期待した結果を与える場合に役立ちます –

+0

最も外側の配列はparent_id "0"でなければなりません。その中にparent_idに基づく子がなければなりません。 parent_idは、その親のcategory_ifにほかならない。 @SahilGulati –

答えて

0

機能

function get_childs($arr, $parent_id = 0) { 
    $res = array(); 
    foreach ($arr as $row) { 
     if ($parent_id == $row['parent_id']) { 
       $childs = get_childs($arr, $row['category_id']); 
       $row['childs'] = $childs; 
       $res[] = $row; 
     } 
    } 
    return $res; 
} 

を考えてみましょうその後、両親を取得するには、この機能を使用すると、それはあなたがあなたの期待出力を共有することができます

$res = get_childs($arr); 
+0

ありがとうございます。出来た !! –