我有一个查询,我编写一个查询并在codeigniter中连接各种表并获取如下数组:
Array
(
[0] => Array
(
[MRID] => 1
[MSR_Name] => John
[MSR_email] => john@gmail.com
[MSR_mobile] => 9000000008
[MSR_hqdetail] => kjjh
[hqdetail_id] => 1
[doctor_id] => 1
[dr_msl_no] => DOC000012
[doctor_name] => Y K Singh
[dr_email] => yksingh@gmail.com
[chemist_id] => 1
[chemist_name] => Abhishek Srivastava
[chem_email] => abhishek@gmail.com
[chemist_code] => chem001
[chem_mobile] => 2147483647
[prod_id] => 1
[product_name] => Paracitamol
[unit] => 10
[pts] => 20
[scheme] => no scheme
)
[1] => Array
(
[MRID] => 1
[MSR_Name] => John
[MSR_email] => john@gmail.com
[MSR_mobile] => 9000000008
[MSR_hqdetail] => kjjh
[hqdetail_id] => 1
[doctor_id] => 2
[dr_msl_no] => DOC000013
[doctor_name] => Dipak Raichura
[dr_email] => dipakraichura@gmail.com
[chemist_id] => 1
[chemist_name] => Abhishek Srivastava
[chem_email] => abhishek@gmail.com
[chemist_code] => chem001
[chem_mobile] => 2147483647
[prod_id] => 1
[product_name] => Paracitamol
[unit] => 10
[pts] => 20
[scheme] => no scheme
)
)
我想操纵数组并想要像这样的结果
Array
(
[0] => Array
(
[MRID] => 1
[MSR_Name] => John
[MSR_email] => john@gmail.com
[MSR_mobile] => 9000000008
[MSR_hqdetail] => kjjh
[hqdetail_id] => 1
[doctors] => array(
[0] => Array(
[doctor_id] => 1
[dr_msl_no] => DOC000012
[doctor_name] => Y K Singh
[dr_email] => yksingh@gmail.com
[chemist] =>Array(
[0] => Array(
[chemist_id] => 1
[chemist_name] => Abhishek Srivastava
[chem_email] => abhishek@gmail.com
[chemist_code] => chem001
[chem_mobile] => 2147483647
[product] =>Array(
[0] => Array(
[prod_id] => 1
[product_name] => Paracitamol
[unit] => 10
[pts] => 20
[scheme] => no scheme
)
)
)
)
)
[1] => Array(
[doctor_id] => 2
[dr_msl_no] => DOC000013
[doctor_name] => Dipak Raichura
[dr_email] => dipakraichura@gmail.com
[chemist] =>Array(
[0] => Array(
[chemist_id] => 2
[chemist_name] => chemist2
[chem_email] => chemist2@gmail.com
[chemist_code] => chem002
[chem_mobile] => 7656564565
product] =>Array(
[0] => Array(
[prod_id] => 2
[product_name] => Crocin
[unit] => 10
[pts] => 20
[scheme] => no scheme
)
)
)
)
)
)
)
)
请帮助我如何才能得到正确的结果,提前致谢。
答案 0 :(得分:0)
CodeIgniter Array Helper可以解决这些问题。在此之前需要加载它。
$this->load->helper('array');
Array Helper可以使用一组键从现有数组中提取新数组。所以制作你感兴趣的键的数组。
$id = ['MRID', 'MSR_Name', 'MSR_email', 'MSR_mobile', 'MSR_hqdetail', 'hqdetail_id'];
$aDoc = ['doctor_id', 'dr_msl_no', 'doctor_name', 'dr_email'];
$aChemist = [ 'chemist_id', 'chemist_name', 'chem_email', 'chemist_code', 'chem_mobile'];
$aproduct = [ 'prod_id', 'product_name', 'unit', 'pts', 'scheme'];
然后提取子阵列并重新组合它们以满足您的规范。
下面使用的变量$results
是从查询返回的数组。
foreach($results as $value)
{
$chemist = elements($aChemist, $value);
$chemist['product'] = elements($aproduct, $value);
$doctor = elements($aDoc, $value);
$doctor['chemist'] = $chemist;
$doctors[] = $doctor;
}
$MRID[] = elements($id, $results[0]);
$MRID['doctors'] = $doctors;
数组$MIRD
现在应包含您指定的结果。
如果MRID
中有多个$results
,则需要额外的代码才能单独提取它们。
虽然这是相对紧凑的,但我倾向于认为您可能想要设计一个查询语句,以产生更接近您最终需求的结果。