Array Format

1.

I/P:

$Nested = [ 
            [ 'id'=>'20098', 'model'=>'FT096', 'grade'=>'B', 'productionAt'=>'Vapi' ],
            [ 'id'=>'38920', 'model'=>'AG654', 'grade'=>'B', 'productionAt'=>'Vapi' ],
            [ 'id'=>'43892', 'model'=>'KE534', 'grade'=>'A', 'productionAt'=>'Vapi' ]
           ];
 var_dump($Nested);

O/P:

array(3) {
     [0]=> array(4) {
          ["id"]=> string(5) "20098" 
          ["model"]=> string(5) "FT096"
          ["grade"]=> string(1) "B"
          ["productionAt"]=> string(4) "Vapi" 
          }
     [1]=> array(4) {
          ["id"]=> string(5) "38920"
          ["model"]=> string(5) "AG654"
          ["grade"]=> string(1) "B"
          ["productionAt"]=> string(4) "Vapi" 
          }
     [2]=> array(4) { 
          ["id"]=> string(5) "43892"
          ["model"]=> string(5) "KE534"
          ["grade"]=> string(1) "A"
          ["productionAt"]=> string(4) "Vapi" 
          } 
     }

2.

I/P:

$Nested = [ 
            [ 'id'=>'20098', 'model'=>'FT096', 'grade'=>'B', 'productionAt'=>'Vapi' ],
            [ 'id'=>'38920', 'model'=>'AG654', 'grade'=>'B', 'productionAt'=>'Vapi' ],
            [ 'id'=>'43892', 'model'=>'KE534', 'grade'=>'A', 'productionAt'=>'Vapi' ]
           ];
 foreach($Nested as $k => $v){
   $Nested[$k]['Status'] = 'Release'; 
 }
 var_dump($Nested);

O/P:

array(3) { 
         [0]=> array(5) { ["id"]=> string(5) "20098" 
                          ["model"]=> string(5) "FT096" 
                          ["grade"]=> string(1) "B" 
                          ["productionAt"]=> string(4) "Vapi" 
                          ["Status"]=> string(7) "Release" 
                         } 
         [1]=> array(5) { ["id"]=> string(5) "38920" 
                          ["model"]=> string(5) "AG654" 
                          ["grade"]=> string(1) "B" 
                          ["productionAt"]=> string(4) "Vapi" 
                          ["Status"]=> string(7) "Release" 
                         } 
         [2]=> array(5) { ["id"]=> string(5) "43892" 
                          ["model"]=> string(5) "KE534" 
                          ["grade"]=> string(1) "A" 
                          ["productionAt"]=> string(4) "Vapi" 
                          ["Status"]=> string(7) "Release" 
                         } 
         }

3.

I/P:

$Nested = [ 
           [ 'id'=>'20098', 'model'=>'FT096', 'grade'=>'B', 'productionAt'=>'Vapi' ],
           [ 'id'=>'38920', 'model'=>'AG654', 'grade'=>'B', 'productionAt'=>'Vapi' ],
           [ 'id'=>'43892', 'model'=>'KE534', 'grade'=>'A', 'productionAt'=>'Vapi' ]
          ];
$groupByGrade = [];
foreach($Nested as $k => $v){
 $groupByGrade[$v['grade']][] = $v;  
}
var_dump($groupByGrade);

O/P:

array(2) { 
     ["B"]=> array(2) { 
         [0]=> array(4) { 
                         ["id"]=> string(5) "20098" 
                         ["model"]=> string(5) "FT096" 
                         ["grade"]=> string(1) "B" 
                         ["productionAt"]=> string(4) "Vapi" 
                         } 
         [1]=> array(4) {
                         ["id"]=> string(5) "38920"
                         ["model"]=> string(5) "AG654"
                         ["grade"]=> string(1) "B"
                         ["productionAt"]=> string(4) "Vapi" 
                         } 
         } 
     ["A"]=> array(1) { 
         [0]=> array(4) {
                         ["id"]=> string(5) "43892"
                         ["model"]=> string(5) "KE534"
                         ["grade"]=> string(1) "A"
                         ["productionAt"]=> string(4) "Vapi"
                         } 
         } 
     }

4.

I/P:

$Nested = [ 
            [ 'id'=>'20098', 'model'=>'FT096', 'grade'=>'B', 'productionAt'=>'Goa' ],
            [ 'id'=>'38920', 'model'=>'AG654', 'grade'=>'B', 'productionAt'=>'Vapi' ],
            [ 'id'=>'80098', 'model'=>'F9096', 'grade'=>'A', 'productionAt'=>'Thane' ],
            [ 'id'=>'18920', 'model'=>'AG654', 'grade'=>'B', 'productionAt'=>'Vapi' ], 
            [ 'id'=>'43892', 'model'=>'KE534', 'grade'=>'A', 'productionAt'=>'Vapi' ]
           ];
 $groupByGrade = [];
 foreach($Nested as $k => $v){
   $groupByGrade[$v['grade']][$v['productionAt']][] = $v;
 }
 var_dump($groupByGrade);

O/P:

array(2) { 
             ["B"]=> array(2) {
                 ["Goa"]=> array(1) { 
                     [0]=> array(4) { ["id"]=> string(5) "20098"
                                      ["model"]=> string(5) "FT096"
                                      ["grade"]=> string(1) "B"
                                      ["productionAt"]=> string(3) "Goa"
                                     } 
                                 }
                 ["Vapi"]=> array(2) {
                     [0]=> array(4) { ["id"]=> string(5) "38920"
                                      ["model"]=> string(5) "AG654"
                                      ["grade"]=>    string(1) "B"
                                      ["productionAt"]=> string(4) "Vapi"
                                     } 
                     [1]=> array(4) { ["id"]=> string(5) "18920"     
                                      ["model"]=> string(5) "AG654" 
                                      ["grade"]=> string(1) "B" 
                                      ["productionAt"]=> string(4) "Vapi" 
                                     } 
                                 }
                 }
             ["A"]=> array(2) {
                 ["Thane"]=> array(1) {
                     [0]=> array(4) { ["id"]=> string(5) "80098"
                                      ["model"]=> string(5) "F9096"
                                      ["grade"]=> string(1) "A"
                                      ["productionAt"]=> string(5) "Thane"
                                     }
                                 }
                 ["Vapi"]=> array(1) {
                     [0]=> array(4) { ["id"]=> string(5) "43892"
                                      ["model"]=> string(5) "KE534"
                                      ["grade"]=> string(1) "A"
                                      ["productionAt"]=> string(4) "Vapi" 
                                     } 
                                 } 
                 } 
         }

1 thought on “Array Format”

Leave a Comment

Your email address will not be published. Required fields are marked *