gpt4 book ai didi

php - 如何在codeigniter中将数组创建为数组

转载 作者:行者123 更新时间:2023-11-29 15:42:34 26 4
gpt4 key购买 nike

我为集合类别创建了一个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;

insert into `crm_category_collection`(`category_collection_id`,`text`,`category_image`,`status`,`collection_id`,`category_id`) values (61,'testerrrdsfdsf','b80314b0d1235ced3fef2b5ef55ff2d2download-(5).jpeg',NULL,33,3),(62,'testw','c3dd2c871fcb4f87f416473a45aedabedownload-(5).jpeg',NULL,33,11),(63,'test78','22278fa17bf50da7b9ec408114d6b092download-(4).jpeg',NULL,33,12);

这是我的 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")),
);

$rs = $this->apis_model->JoinFetch($main_table_collection, $join_tables_collection, $condition_collection, array($default_sort_column_collection => $default_sort_order_collection)); // fetch query
$collection_data = $this->apis_model->MySqlFetchRow($rs, "array"); // fetch result

if (empty($collection_data)) {
$collection_data = array();
} else {
foreach ($collection_data as $key => $val) {
unset($collection_data[$key]);
$temp=array();
$collection_data[$key]['heading']=$val['heading'];
$collection_data[$key]['group_id']=$val['group_id'];
$collection_data[$key]['group_name']=$val['group_name'];
$temp['text']=$val['text'];
$temp['category_id']=$val['category_id'];

if (!empty($val['collection_image']) && $val['collection_image'] != "") {
$collection_image = "";
$collection_image = base_url() . "images/collections/" . $val['collection_image'];
//echo $icon_image; exit;
$collection_data[$key]['collection_image'] = $collection_image;
}
if (!empty($val['category_image']) && $val['category_image'] != "") {
$category_image = "";
$category_image = base_url() . "images/collections/" . $val['category_image'];
//echo $category_image; exit;
$temp['category_image'] = $category_image;
$collection_data[$key]['category'][] = $temp;

// print_r($collection_data);
}
}
// exit();
}
$result['collection'] = $collection_data;

而这个结果将会是这样的。

"collection-category": [
{
"heading": "test3",
"group_id": "1",
"group_name": "group1",
"collection_image": "http://localhost:8000/images/collections/30d79a495652f762bbe365df59dbef3b.jpeg",
"category": [
{
"collection_id": "33",
"text": "dfsfdsfd",
"category_id": "11",
"category_image": "http://localhost:8000/images/collections/219cbf9b9bdd8f6581801310dd312ef5download-(6).jpeg"
}
]
},
{
"heading": "test3",
"group_id": "1",
"group_name": "group1",
"collection_image": "http://localhost:8000/images/collections/30d79a495652f762bbe365df59dbef3b.jpeg",
"category": [
{
"collection_id": "33",
"text": "testerrrdsfdsf",
"category_id": "3",
"category_image": "http://localhost:8000/images/collections/b80314b0d1235ced3fef2b5ef55ff2d2download-(5).jpeg"
}
]
}
]

但是我有这种类型的结果,因为我的集合是一个,但集合中包含多个类别。

"collection-category": [
{
"heading": "test3",
"group_id": "1",
"group_name": "group1",
"collection_image": "http://localhost:8000/images/collections/30d79a495652f762bbe365df59dbef3b.jpeg",
"category": [
{
"collection_id": "33",
"text": "hi",
"category_id": "11",
"category_image": "http://localhost:8000/images/collections/219cbf9b9bdd8f6581801310dd312ef5download-(6).jpeg"
},
{
"collection_id": "34",
"text": "hello",
"category_id": "3",
"category_image": "http://localhost:8000/images/collections/b80314b0d1235ced3fef2b5ef55ff2d2download-(5).jpeg"
},
{
"collection_id": "35",
"text": "teste123",
"category_id": "3",
"category_image": "http://localhost:8000/images/collections/b80314b0d1235ced3fef2b5ef55ff2d2download-(5).jpeg"
}
]
}
]

请帮助我,我无法理解如何创建这种类型的响应。

最佳答案

@vaibhavi sikligar

此处的代码描述了逻辑应如何在结果集上运行以获得所需的格式,并使用一些与查询结果匹配的虚拟数组数据。

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

您可以在代码中尝试此操作。

关于php - 如何在codeigniter中将数组创建为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com