[英]How to merge a 2 dimensional array into an existing array from inside a loop in php
I am having the following function, where I am getting list of ultrasounds for a given surgery by using 我具有以下功能,可以通过使用以下命令获取给定手术的超声列表
$hmo_claim_generate = new HMOClaimGenerate();
$services = $hmo_claim_generate->generateUltrasounds($hospital_id, $patient_id, "CONSULTATION", $id);
Below is the function itself which is part of a class file HMOClaimGenerate. 下面是函数本身,它是类文件HMOClaimGenerate的一部分。
// Generate Ultrasounds
//*********************************************************************************
public function generateUltrasounds($hospital_id, $patient_id, $service_type, $service_type_id)
{
$mysql = new Mysql();
$hospital_id = intval($hospital_id);
$patient_id = intval($patient_id);
$service_type_id = intval($service_type_id);
$sql = "SELECT ultrasound_id FROM ". TABLE_HOSPITALS_ULTRASOUNDS ." WHERE hospital_id=$hospital_id AND patient_id=$patient_id AND request_from_type = '$service_type' AND request_from_id=$service_type_id AND payment_type = 'HMO' AND fee_status = 'BILLED' AND fee > 0";
$totalRecords = $mysql->rowCount($query = $mysql->query($sql));
if ($totalRecords > 0)
{
while($row = $mysql->fetchArray($query))
{
$ultrasound_id = $row["ultrasound_id"];
$services["ultrasounds"][$ultrasound_id] = $ultrasound_id;
}
}
return $services;
}
And this is the output 这是输出
Array
(
[ultrasounds] => Array
(
[102] => 102
[203] => 203
)
)
Now inside the function below, I am calling the above function 现在在下面的函数中,我正在调用上面的函数
// Generate Surgeries
//*********************************************************************************
public function generateSurgeries($hospital_id, $patient_id, $service_type, $service_type_id)
{
$mysql = new Mysql();
$string = new String();
$hospital_id = intval($hospital_id);
$patient_id = intval($patient_id);
$service_type_id = intval($service_type_id);
$sql = "SELECT surgery_id FROM ". TABLE_HOSPITALS_SURGERIES ." WHERE hospital_id=$hospital_id AND patient_id=$patient_id AND request_from_type = '$service_type' AND request_from_id=$service_type_id";
$totalRecords = $mysql->rowCount($query = $mysql->query($sql));
if ($totalRecords > 0)
{
$count = 0;
while($row = $mysql->fetchArray($query))
{
$surgery_id = $row["surgery_id"];
$services["surgeries"][$surgery_id] = $surgery_id;
// Calling the function as displayed on top
$ultrasounds = $this->generateUltrasounds($hospital_id, $patient_id, "SURGERY", $surgery_id);
}
}
// Trying to merge the records from surgeries and ultrasounds into single array
$services = array_merge($services, $ultrasounds);
return $services;
}
Ultrasound here is a child or sub service falling under Surgery and I am trying to merge both the surgery and ultrasound into a single array but it has to be done from inside the while loop so that I can get the following result: 此处的超声检查属于儿童或子服务部门,我正在尝试将外科手术和超声检查合并为一个阵列,但是必须从while循环内部完成,这样才能得到以下结果:
Array
(
[surgeries] => Array
(
[1] => 1
[4] => 4
[10] => 10
)
[ultrasounds] => Array
(
[102] => 102
[203] => 203
)
)
Simple speaking I want to combine surgeries and ultrasounds in a single array called $services from where I can access the respective services such as 简单地说,我想将外科手术和超声检查结合在一个称为$ services的阵列中,从那里我可以访问相应的服务,例如
print_r($services["surgeries"][]);
print_r($services["ultrasounds"][]);
Please help me in sorting this issue. 请帮助我解决此问题。 Tried lots of things but nothing is working at all for now.
尝试了很多东西,但暂时没有任何效果。
Awaiting your inputs. 等待您的输入。
You can merge them using a FOREACH. 您可以使用FOREACH合并它们。
$services='';
$i=0;
foreach($ultrasounds as $row){
$services[$i]['ultrasounds']=$row;
$i++;
}
$i=0;
foreach($surgeries as $row){
$services[$i]['surgeries']=$row;
$i++;
}
Where the variables ultrasounds and surgeries are the return from the query. 超声和手术变量是查询的返回值。
You need to get a single array from generateUltrasounds
您需要从
generateUltrasounds
获得单个数组
$ultrasounds = [];
while ($row = $mysql->fetchArray($query)) {
$ultrasound_id = $row["ultrasound_id"];
$ultrasounds[$ultrasound_id] = $ultrasound_id;
}
return $ultrasounds;
And then, in generateSurgeries
, when you call generateUltrasounds
you can do like you do for ['surgeries']
2 lines above. 然后,在
generateSurgeries
,当您调用generateUltrasounds
您可以像对上面两行的['surgeries']
一样进行操作。
while ($row = $mysql->fetchArray($query)) {
$surgery_id = $row["surgery_id"];
$services["surgeries"][$surgery_id] = $surgery_id;
// Calling the function as displayed on top
$services["ultrasounds"][] = $this->generateUltrasounds($hospital_id, $patient_id, "SURGERY", $surgery_id);
}
return $services;
You no longer need to merge. 您不再需要合并。 This code is not tidy at all, but it should solve your problem.
这段代码一点也不整洁,但是可以解决您的问题。
LE - If you are still not happy with what's inside $services['ultrasounds']
, you can do LE-如果您仍然对
$services['ultrasounds']
内容不满意,可以这样做
while ($row = $mysql->fetchArray($query)) {
$surgery_id = $row["surgery_id"];
$services["surgeries"][$surgery_id] = $surgery_id;
// Calling the function as displayed on top
$services["ultrasounds"][] = $this->generateUltrasounds($hospital_id, $patient_id, "SURGERY", $surgery_id);
}
$services['ultrasounds'] = call_user_func_array('array_merge', $services['ultrasounds']);
return $services;
which will merge all the elements inside ['ultrasounds']
into a single array. 这会将
['ultrasounds']
所有元素合并到一个数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.