如何在codignitor中将数组创建为数组

我为 collection-category 创建了一个 API。但是我在将数组转换为数组时遇到了问题。


我已经尝试取消设置旧数组并附加到新数组中,但我没有得到正确的数组。


我的收藏表是这样的,


CREATE TABLE `crm_collection` (

 `collection_id` int(11) NOT NULL AUTO_INCREMENT,

 `heading` varchar(255) DEFAULT NULL,

 `collection_image` varchar(255) DEFAULT NULL,

 `status` enum('Active','In-active') DEFAULT NULL,

 `group_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`collection_id`)

 ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=latin1;


 insert  into   `crm_collection`(`collection_id`,`heading`,`collection_image`,`status`,`group_id`) values (33,'test3','30d79a495652f762bbe365df59dbef3b.jpeg','Active',1),(34,'test4','7cc28028d0684d9bb58e285434002758.jpeg','In-active',3);

我的收藏类别表如下所示,


CREATE TABLE `crm_category_collection` (

  `category_collection_id` int(11) NOT NULL AUTO_INCREMENT,

  `text` varchar(255) DEFAULT NULL,

  `category_image` varchar(255) DEFAULT NULL,

  `status` enum('Active','In-active') DEFAULT NULL,

  `collection_id` int(11) DEFAULT NULL,

  `category_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`category_collection_id`)

) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1;


这是我的 API 代码。


$table_collection = "crm_collection";

     $table_collection_id = 'tl.collection_id';

     $default_sort_column_collection = 'tl.collection_id';

     $default_sort_order_collection = 'desc';

     $condition_collection = "1=1";


    $main_table_collection = array("$table_collection tl", array("tl.collection_id", "tl.heading", "tl.collection_image", "tl.group_id"));

    $join_tables_collection = array(

        array("left", "tbl_menu tm", "tm.id = tl.group_id", array("tm.menu_name as group_name","tm.id as group_id")),

        array("left", "crm_category_collection cc", "cc.collection_id = tl.collection_id", array("cc.text","cc.category_image","cc.category_id")),

    );



缥缈止盈
浏览 109回答 1
1回答

慕桂英546537

这里的代码描述了逻辑应该如何在您的结果集上运行以获得所需的格式,其中一些虚拟数组数据与您的查询结果匹配。$test = array( array( "collection_id" => 33, 'heading' => 'test3', 'category_id' => 3, 'text' => 'dfsfdsfd1' ),    array( "collection_id" => 33, 'heading' => 'test3', 'category_id' => 4, 'text' => 'dfsfdsfd2' ),    array( "collection_id" => 34, 'heading' => 'test4', 'category_id' => 5, 'text' => 'dfsfdsfd3' ),    array( "collection_id" => 34, 'heading' => 'test4', 'category_id' => 6, 'text' => 'dfsfdsfd4' ));$res= array();foreach ($test as $key => $value) {    if( !isset($res[$value['collection_id']]) ){        $res[$value['collection_id']] = array();        $res[$value['collection_id']]['collection_id'] = $value['collection_id'];        $res[$value['collection_id']]['heading'] = $value['heading'];    }               $colarr = isset($res[$value['collection_id']]['category']) && is_array($res[$value['collection_id']]['category']) ? $res[$value['collection_id']]['category'] : array();     $colarr[] = array( 'category_id' => $value['category_id'], 'text' => $value['text'] );    $res[$value['collection_id']]['category'] = $colarr;                    }$output = array();$output['collection-category'] = array_values($res);echo json_encode($output);你可以在你的代码中试试这个。
打开App,查看更多内容
随时随地看视频慕课网APP