• Breaking News

    Saturday, 6 February 2016

    Multi Dimension Array Problem

    The problem is related to array:-

    Like we have an array that contain some details of students’ age with their standard and number of records according to group by age and standard from the database.
    Like we have get data from database:-
    Age
    Standard/class
    Number Of Records
    12
    6
    2
    13
    6
    1
    14
    7
    1
    23
    6
    2
    23
    12
    5
    24
    12
    1
    25
    12
    2
    Age Group like this
    10 => 1-10
    20 =>11-20
    30 => 21-30
    40 => 31-40
    50 => 41-50

    I have an array of age group:
    Like ageGroupArray = 10, 20, 30, 40, 50;
    Then I want the result like this in array:
    the array is getting on the bases of the age's and the index of array is age and where the ageGroupArray contain the values of sum of number of records.
    //here [6] represent the index of Count array and 6 is the standard of the student

    //as Input Array
    details     = {
    [0]=> {  [age]=>12
      [Standard]=>6
      [NumberOfRecords]=>2
             }
    [1]=> {  [age]=>13
      [Standard]=>6
      [NumberOfRecords]=>1
             }
    [2]=> {  [age]=>14
      [Standard]=>7
      [NumberOfRecords]=>1
             }
    [3]=> {  [age]=>23
      [Standard]=>6
      [NumberOfRecords]=>2
             }
    [4]=> {  [age]=>23
    [Standard]=>12
    [NumberOfRecords]=>5
            }
    [5]=> {  [age]=>24
      [Standard]=>12
      [NumberOfRecords]=>1
              }
    [6]=> {  [age]=>25
      [Standard]=>12
      [NumberOfRecords]=>2
             }
                    }

    ageArray = (10, 20, 30, 40, 50);//this is an array of age groups 

    //as output Array
    count = {
            [6]=> {  10=>0
    20=>3 //here we add the the number of records (2+1=3) according to standard
    30=>2
    40=>0
    50=>0
            }

             [7]=> {  10=>0
    20=>1
    30=>0
    40=>0
    50=>0
            }
           [12]=> {  10=>0
    20=>0
    30=>8
    40=>0
    50=>0
            }

    Here Is the Solution for that:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12  
    for ($j = 0; $j < sizeof($details); $j++) {
     $std = $details[$j]['standard'];
     for ($i = 0; $i < sizeof($ageArray); $i++) {
      if($ageArray[$i-1] < $details[$j]['age'] && $details[$j]['age'] < $ageArray[$i]  ){
       $count[$std][$ageArray[$i]] += $details[$j]['NumberOfRecords'];
      }else {
       $count[$std][$ageArray[$i]]  +=  0;
      }
     }
    }
    print_r($details);
    print_r($count);
    

    No comments:

    Post a Comment