简体   繁体   中英

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.

    // 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:

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

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.

$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

$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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM