3

I have an array of php like this

Array
(
    [0] => Array
        (
            [Sr_No] => 1
            [Asan_Category] => Warm-Up
            [Asan_Cat_Val] => 8
            [Asan_Sub_Category] => Ankle
            [Asan_Sub_Cat_Val] => 35
            [Asan_Name] => General Ankle Warm up
            [Asan_Name_Val] => 447
            [Prescribed_Steps] => 40
            [Prescribed_Ratio] => 00
            [Actual_Steps] => 12
            [Actual_Ratio] => 0
        )
 [1] => Array
        (
            [Sr_No] => 2
            [Asan_Category] => Warm-Up
            [Asan_Cat_Val] => 8
            [Asan_Sub_Category] => Knee
            [Asan_Sub_Cat_Val] => 111
            [Asan_Name] => General knee warm up
            [Asan_Name_Val] => 464
            [Prescribed_Steps] => 20
            [Prescribed_Ratio] => 00
            [Actual_Steps] => 14
            [Actual_Ratio] => 0
        )
[2] => Array
        (
            [Sr_No] => 1
            [Asan_Category] => Warm-Up
            [Asan_Cat_Val] => 8
            [Asan_Sub_Category] => Ankle
            [Asan_Sub_Cat_Val] => 35
            [Asan_Name] => General Ankle Warm up
            [Asan_Name_Val] => 447
            [Prescribed_Steps] => 40
            [Prescribed_Ratio] => 00
            [Actual_Steps] => 10
            [Actual_Ratio] => 0
        )
[3] => Array
        (
            [Sr_No] => 2
            [Asan_Category] => Warm-Up
            [Asan_Cat_Val] => 8
            [Asan_Sub_Category] => Knee
            [Asan_Sub_Cat_Val] => 111
            [Asan_Name] => General knee warm up
            [Asan_Name_Val] => 464
            [Prescribed_Steps] => 20
            [Prescribed_Ratio] => 00
            [Actual_Steps] => 9
            [Actual_Ratio] => 0
        )
)

The desired output I want 

Array
(
    [0] => Array
        (
            [Asan_Id] => 447
            [Asan_Category] => Warm-Up
            [Asan_Sub_Category] => Ankle
            [Asan_Name] => General Ankle Warm up
            [Prescribed_Steps] => 40
            [Prescribed_Ratio] => 00
            [Total_Steps] => 22
        )

    [1] => Array
        (
            [Asan_Id] => 464
            [Asan_Category] => Warm-Up
            [Asan_Sub_Category] => Knee
            [Asan_Name] => General knee warm up
            [Prescribed_Steps] => 20
            [Prescribed_Ratio] => 00
            [Total_Steps] => 23
        )
)

I want those data who are repeating become one but their different actual steps become total steps with their sum. Please help me because I have tried some code but did not success like this

    $asan=[];
    $total_steps=0;
    foreach ($aasan_details as $key) {

        $total_steps += $key['Actual_Steps'];
        if(!in_array($key['Asan_Name_Val'],$asan))
        {
            $asan[] = $key['Asan_Name_Val'];

          $lookup[] = array("Asan_Id"=>$key['Asan_Name_Val'],
                      "Asan_Category"=>$key['Asan_Category'],
                      "Asan_Sub_Category"=>$key['Asan_Sub_Category'],
                      "Asan_Name"=>$key['Asan_Name'],
                      "Prescribed_Steps"=>$key['Prescribed_Steps'],
                      "Prescribed_Ratio"=>$key['Prescribed_Ratio'],
                      'Total_Steps'=>$total_steps);
        }


    }

It is not working , what should I do guys , please help me out

where $aasan_details is the array which I showed above and the in lookup array I am getting uniqe values but not getting their total

3
  • Excuse me, but total steps contains the sum by id.. Commented Sep 20, 2019 at 14:25
  • Can you provide a var_export output of the dummy data? Commented Sep 20, 2019 at 14:55
  • Asan Id is the asan name val Commented Sep 21, 2019 at 5:13

2 Answers 2

2

The best solution would be to move this into a database and use a query to perform that exact operation for you.

But if you have to do it in code, you could use a keyed array to basically group them up by however many fields need to match for them to be conjoined:

e.g.

foreach ..
$consolidated[$item['Asan_Cat_Val']][$item['Asan_Sub_Cat_Val']][] = $item;

Then make a couple of nested loops to go across this and sum everything up.

Sign up to request clarification or add additional context in comments.

Comments

0

There a few useful techniques to explain here...

  1. You can reduce eye-strain in your looped array declarations by establishing a collection of keys that you know you want to retain in (copy to) the output array. This spares you having to type out each $keyName => $row[$keyName], like this (but ultimately make yourself happy).

  2. Declare temporary associative keys (using $id) while building your output array to allow isset() to make super fast checks for a pre-existing group.

  3. If a group is not yet set, then store the data with the new keys along with the data with the original keys. No arithmetic is necessary. The + symbols in my snippet are not "addition operators", they are "array union operators" -- they are merging the three arrays without a function call (otherwise array_merge() would do the same).

  4. When finished iterating the array, you may choose to re-index the result (remove the temporary associative keys) by calling array_values(). If you are not bothered by the existence of these temporary keys, you can omit the function call.

  5. If a group has been encountered before, you only need to modify the Total_steps. Use an addition-assignment operator (+=) for the briefest syntax.

Code: (Demo)

$keysToKeep = array_flip(['Asan_Category', 'Asan_Sub_Category', 'Asan_Name', 'Prescribed_Steps', 'Prescribed_Ratio']);

foreach ($array as $row) {
    $id = $row['Asan_Name_Val'];
    if (!isset($result[$id])) {
        $result[$id] = ['Asan_Id' => $id] + array_intersect_key($row, $keysToKeep) + ['Total_Steps' => $row['Actual_Steps']];
    } else {
        $result[$id]['Total_Steps'] += $row['Actual_Steps'];
    }
}
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'Asan_Id' => '447',
    'Asan_Category' => 'Warm-Up',
    'Asan_Sub_Category' => 'Ankle',
    'Asan_Name' => 'General Ankle Warm up',
    'Prescribed_Steps' => '40',
    'Prescribed_Ratio' => '00',
    'Total_Steps' => 22,
  ),
  1 => 
  array (
    'Asan_Id' => '464',
    'Asan_Category' => 'Warm-Up',
    'Asan_Sub_Category' => 'Knee',
    'Asan_Name' => 'General knee warm up',
    'Prescribed_Steps' => '20',
    'Prescribed_Ratio' => '00',
    'Total_Steps' => 23,
  ),
)

1 Comment

@satya You have asked a couple of php question, now, that have included sample input arrays. Please always provide your sample array data as var_export() output or as a json string. This way volunteers don't have to toil with converting your data into a code-ready structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.