how to get the array key values and count the values in one array object












-3















I have doubt in php arrays get the array key value and count the same key repeated in php array.
Here am posting my array object output through the one variable am getting this data.
Here my requirement is in this array i have [room_type] and [meal_type].




Here [room_type] => APARTMENT ONE BEDROOM is 3 times came in this
array. so my required result is 3 x APARTMENT ONE BEDROOM Here
[room_type] => APARTMENT TWO BEDROOM is 2 times came in this array.
so my required result is 2 x APARTMENT TWO BEDROOM Here [room_type]
=> APARTMENT THREE BEDROOM is 1 time came in this array. so my required result is 1 x APARTMENT THREE BEDROOM




$room_details =  
Array
(
[0] => stdClass Object
(
[id] => 4508
[api] => hotelbeds
[room_no] => 1
[room_type] => APARTMENT ONE BEDROOM
[meal_type] => ONE ROOM
[updated_on] => 2019-01-16 13:03:17
)

[1] => stdClass Object
(
[id] => 4509
[room_no] => 2
[room_type] => APARTMENT TWO BEDROOM
[meal_type] => TWO ROOMS
[updated_on] => 2019-01-16 13:03:09
)

[2] => stdClass Object
(
[id] => 4510
[room_no] => 3
[room_type] => APARTMENT ONE BEDROOMS
[meal_type] => TWO ROOMS
[updated_on] => 2019-01-16 13:03:38
)

[3] => stdClass Object
(
[id] => 4511
[room_no] => 3
[room_type] => APARTMENT TWO BEDROOMS
[meal_type] => TWO ROOMS
[updated_on] => 2019-01-16 13:03:38
)

[4] => stdClass Object
(
[id] => 4512
[room_no] => 3
[room_type] => APARTMENT THREE BEDROOMS
[meal_type] => THREE ROOMS
[updated_on] => 2019-01-16 13:03:38
)

[5] => stdClass Object
(
[id] => 4513
[room_no] => 3
[room_type] => APARTMENT ONE BEDROOMS
[meal_type] => ONE ROOM
[updated_on] => 2019-01-16 13:03:38
)
)


This result i want to show in One div tag like this.



<div class="roomtype">
<p>
3 x APARTMENT ONE BEDROOM
2 x APARTMENT TWO BEDROOM
1 x APARTMENT THREE BEDROOM
</p>
</div>


And [meal_type] => ONE ROOM came in this array 2 times so result is 2 x ONE ROOM
[meal_type] => TWO ROOMS came in this array 3 times so result is 3 x TWO ROOMS
[meal_type] => THREE ROOMS came in this array 1 time so result is 1 x THREE ROOMS
This result i want to show in Another Div Tag like this .



<div class="mealtype">
<p>
2 x ONE ROOM
3 x TWO ROOMS
1 x THREE ROOMS
</p>
</div>


I tried below code not working , can any one please help on this.



                                        For Meal Type 
--------------
$countedValues = array_count_values($room_details->meal_type);
//Build strings from value and display them.
foreach( $countedValues as $roomType=>$count ){
echo $count . ' x ' . $Mealtype . '<br />';
}

For Room Type
--------------
$countedValues = array_count_values($room_details->room_type);
//Build strings from value and display them.
foreach( $countedValues as $roomType=>$count ){
echo $count . ' x ' . $roomType . '<br />';
}









share|improve this question



























    -3















    I have doubt in php arrays get the array key value and count the same key repeated in php array.
    Here am posting my array object output through the one variable am getting this data.
    Here my requirement is in this array i have [room_type] and [meal_type].




    Here [room_type] => APARTMENT ONE BEDROOM is 3 times came in this
    array. so my required result is 3 x APARTMENT ONE BEDROOM Here
    [room_type] => APARTMENT TWO BEDROOM is 2 times came in this array.
    so my required result is 2 x APARTMENT TWO BEDROOM Here [room_type]
    => APARTMENT THREE BEDROOM is 1 time came in this array. so my required result is 1 x APARTMENT THREE BEDROOM




    $room_details =  
    Array
    (
    [0] => stdClass Object
    (
    [id] => 4508
    [api] => hotelbeds
    [room_no] => 1
    [room_type] => APARTMENT ONE BEDROOM
    [meal_type] => ONE ROOM
    [updated_on] => 2019-01-16 13:03:17
    )

    [1] => stdClass Object
    (
    [id] => 4509
    [room_no] => 2
    [room_type] => APARTMENT TWO BEDROOM
    [meal_type] => TWO ROOMS
    [updated_on] => 2019-01-16 13:03:09
    )

    [2] => stdClass Object
    (
    [id] => 4510
    [room_no] => 3
    [room_type] => APARTMENT ONE BEDROOMS
    [meal_type] => TWO ROOMS
    [updated_on] => 2019-01-16 13:03:38
    )

    [3] => stdClass Object
    (
    [id] => 4511
    [room_no] => 3
    [room_type] => APARTMENT TWO BEDROOMS
    [meal_type] => TWO ROOMS
    [updated_on] => 2019-01-16 13:03:38
    )

    [4] => stdClass Object
    (
    [id] => 4512
    [room_no] => 3
    [room_type] => APARTMENT THREE BEDROOMS
    [meal_type] => THREE ROOMS
    [updated_on] => 2019-01-16 13:03:38
    )

    [5] => stdClass Object
    (
    [id] => 4513
    [room_no] => 3
    [room_type] => APARTMENT ONE BEDROOMS
    [meal_type] => ONE ROOM
    [updated_on] => 2019-01-16 13:03:38
    )
    )


    This result i want to show in One div tag like this.



    <div class="roomtype">
    <p>
    3 x APARTMENT ONE BEDROOM
    2 x APARTMENT TWO BEDROOM
    1 x APARTMENT THREE BEDROOM
    </p>
    </div>


    And [meal_type] => ONE ROOM came in this array 2 times so result is 2 x ONE ROOM
    [meal_type] => TWO ROOMS came in this array 3 times so result is 3 x TWO ROOMS
    [meal_type] => THREE ROOMS came in this array 1 time so result is 1 x THREE ROOMS
    This result i want to show in Another Div Tag like this .



    <div class="mealtype">
    <p>
    2 x ONE ROOM
    3 x TWO ROOMS
    1 x THREE ROOMS
    </p>
    </div>


    I tried below code not working , can any one please help on this.



                                            For Meal Type 
    --------------
    $countedValues = array_count_values($room_details->meal_type);
    //Build strings from value and display them.
    foreach( $countedValues as $roomType=>$count ){
    echo $count . ' x ' . $Mealtype . '<br />';
    }

    For Room Type
    --------------
    $countedValues = array_count_values($room_details->room_type);
    //Build strings from value and display them.
    foreach( $countedValues as $roomType=>$count ){
    echo $count . ' x ' . $roomType . '<br />';
    }









    share|improve this question

























      -3












      -3








      -3








      I have doubt in php arrays get the array key value and count the same key repeated in php array.
      Here am posting my array object output through the one variable am getting this data.
      Here my requirement is in this array i have [room_type] and [meal_type].




      Here [room_type] => APARTMENT ONE BEDROOM is 3 times came in this
      array. so my required result is 3 x APARTMENT ONE BEDROOM Here
      [room_type] => APARTMENT TWO BEDROOM is 2 times came in this array.
      so my required result is 2 x APARTMENT TWO BEDROOM Here [room_type]
      => APARTMENT THREE BEDROOM is 1 time came in this array. so my required result is 1 x APARTMENT THREE BEDROOM




      $room_details =  
      Array
      (
      [0] => stdClass Object
      (
      [id] => 4508
      [api] => hotelbeds
      [room_no] => 1
      [room_type] => APARTMENT ONE BEDROOM
      [meal_type] => ONE ROOM
      [updated_on] => 2019-01-16 13:03:17
      )

      [1] => stdClass Object
      (
      [id] => 4509
      [room_no] => 2
      [room_type] => APARTMENT TWO BEDROOM
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:09
      )

      [2] => stdClass Object
      (
      [id] => 4510
      [room_no] => 3
      [room_type] => APARTMENT ONE BEDROOMS
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [3] => stdClass Object
      (
      [id] => 4511
      [room_no] => 3
      [room_type] => APARTMENT TWO BEDROOMS
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [4] => stdClass Object
      (
      [id] => 4512
      [room_no] => 3
      [room_type] => APARTMENT THREE BEDROOMS
      [meal_type] => THREE ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [5] => stdClass Object
      (
      [id] => 4513
      [room_no] => 3
      [room_type] => APARTMENT ONE BEDROOMS
      [meal_type] => ONE ROOM
      [updated_on] => 2019-01-16 13:03:38
      )
      )


      This result i want to show in One div tag like this.



      <div class="roomtype">
      <p>
      3 x APARTMENT ONE BEDROOM
      2 x APARTMENT TWO BEDROOM
      1 x APARTMENT THREE BEDROOM
      </p>
      </div>


      And [meal_type] => ONE ROOM came in this array 2 times so result is 2 x ONE ROOM
      [meal_type] => TWO ROOMS came in this array 3 times so result is 3 x TWO ROOMS
      [meal_type] => THREE ROOMS came in this array 1 time so result is 1 x THREE ROOMS
      This result i want to show in Another Div Tag like this .



      <div class="mealtype">
      <p>
      2 x ONE ROOM
      3 x TWO ROOMS
      1 x THREE ROOMS
      </p>
      </div>


      I tried below code not working , can any one please help on this.



                                              For Meal Type 
      --------------
      $countedValues = array_count_values($room_details->meal_type);
      //Build strings from value and display them.
      foreach( $countedValues as $roomType=>$count ){
      echo $count . ' x ' . $Mealtype . '<br />';
      }

      For Room Type
      --------------
      $countedValues = array_count_values($room_details->room_type);
      //Build strings from value and display them.
      foreach( $countedValues as $roomType=>$count ){
      echo $count . ' x ' . $roomType . '<br />';
      }









      share|improve this question














      I have doubt in php arrays get the array key value and count the same key repeated in php array.
      Here am posting my array object output through the one variable am getting this data.
      Here my requirement is in this array i have [room_type] and [meal_type].




      Here [room_type] => APARTMENT ONE BEDROOM is 3 times came in this
      array. so my required result is 3 x APARTMENT ONE BEDROOM Here
      [room_type] => APARTMENT TWO BEDROOM is 2 times came in this array.
      so my required result is 2 x APARTMENT TWO BEDROOM Here [room_type]
      => APARTMENT THREE BEDROOM is 1 time came in this array. so my required result is 1 x APARTMENT THREE BEDROOM




      $room_details =  
      Array
      (
      [0] => stdClass Object
      (
      [id] => 4508
      [api] => hotelbeds
      [room_no] => 1
      [room_type] => APARTMENT ONE BEDROOM
      [meal_type] => ONE ROOM
      [updated_on] => 2019-01-16 13:03:17
      )

      [1] => stdClass Object
      (
      [id] => 4509
      [room_no] => 2
      [room_type] => APARTMENT TWO BEDROOM
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:09
      )

      [2] => stdClass Object
      (
      [id] => 4510
      [room_no] => 3
      [room_type] => APARTMENT ONE BEDROOMS
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [3] => stdClass Object
      (
      [id] => 4511
      [room_no] => 3
      [room_type] => APARTMENT TWO BEDROOMS
      [meal_type] => TWO ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [4] => stdClass Object
      (
      [id] => 4512
      [room_no] => 3
      [room_type] => APARTMENT THREE BEDROOMS
      [meal_type] => THREE ROOMS
      [updated_on] => 2019-01-16 13:03:38
      )

      [5] => stdClass Object
      (
      [id] => 4513
      [room_no] => 3
      [room_type] => APARTMENT ONE BEDROOMS
      [meal_type] => ONE ROOM
      [updated_on] => 2019-01-16 13:03:38
      )
      )


      This result i want to show in One div tag like this.



      <div class="roomtype">
      <p>
      3 x APARTMENT ONE BEDROOM
      2 x APARTMENT TWO BEDROOM
      1 x APARTMENT THREE BEDROOM
      </p>
      </div>


      And [meal_type] => ONE ROOM came in this array 2 times so result is 2 x ONE ROOM
      [meal_type] => TWO ROOMS came in this array 3 times so result is 3 x TWO ROOMS
      [meal_type] => THREE ROOMS came in this array 1 time so result is 1 x THREE ROOMS
      This result i want to show in Another Div Tag like this .



      <div class="mealtype">
      <p>
      2 x ONE ROOM
      3 x TWO ROOMS
      1 x THREE ROOMS
      </p>
      </div>


      I tried below code not working , can any one please help on this.



                                              For Meal Type 
      --------------
      $countedValues = array_count_values($room_details->meal_type);
      //Build strings from value and display them.
      foreach( $countedValues as $roomType=>$count ){
      echo $count . ' x ' . $Mealtype . '<br />';
      }

      For Room Type
      --------------
      $countedValues = array_count_values($room_details->room_type);
      //Build strings from value and display them.
      foreach( $countedValues as $roomType=>$count ){
      echo $count . ' x ' . $roomType . '<br />';
      }






      php arrays






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 14:07









      NiranjanNiranjan

      186




      186
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Hello Niranjan,



          you can iterate through your array and count this data as followed,



          <?php
          $room_details = array(
          array(
          "id" => 4508,
          "api" => "hotelbeds",
          "room_no" => 1,
          "room_type" => "APARTMENT ONE BEDROOMS",
          "meal_type" => "ONE ROOM",
          "updated_on" => "2019-01-16 13:03:17"
          ),
          array(
          "id" => 4509,
          "room_no" => 2,
          "room_type" => "APARTMENT TWO BEDROOMS",
          "meal_type" => "TWO ROOMS",
          "updated_on" => "2019-01-16 13:03:09"
          ),
          array(
          "id" => 4510,
          "room_no" => 3,
          "room_type" => "APARTMENT ONE BEDROOMS",
          "meal_type" => "TWO ROOMS",
          "updated_on" => "2019-01-16 13:03:38"
          ),
          array(
          "id" => 4511,
          "room_no" => 3,
          "room_type" => "APARTMENT TWO BEDROOMS",
          "meal_type" => "TWO ROOMS",
          "updated_on" => "2019-01-16 13:03:38"
          ),
          array(
          "id" => 4512,
          "room_no" => 3,
          "room_type" => "APARTMENT THREE BEDROOMS",
          "meal_type" => "THREE ROOMS",
          "updated_on" => "2019-01-16 13:03:38"
          ),
          array(
          "id" => 4513,
          "room_no" => 3,
          "room_type" => "APARTMENT ONE BEDROOMS",
          "meal_type" => "ONE ROOM",
          "updated_on" => "2019-01-16 13:03:38"
          )
          );

          $room_types = ;
          $meal_types = ;

          foreach($room_details as $k => $v) {
          if(!array_key_exists($v["room_type"], $room_types)) {
          $room_types[$v["room_type"]] = 1;
          } else {
          $room_types[$v["room_type"]] = $room_types[$v["room_type"]] + 1;
          }

          if(!array_key_exists($v["meal_type"], $meal_types)) {
          $meal_types[$v["meal_type"]] = 1;
          } else {
          $meal_types[$v["meal_type"]] = $meal_types[$v["meal_type"]] + 1;
          }
          }

          print_r($room_types);
          print_r($meal_types);
          ?>


          Now you can access those arrays to fetch count.






          share|improve this answer































            0














            Try something like the following:



            <?php

            $room_details = array(
            (object)array(
            'id' => 4508,
            'api' => 'hotelbeds',
            'room_no' => 1,
            'room_type' => 'APARTMENT ONE BEDROOM',
            'meal_type' => 'ONE ROOM',
            'updated_on' => '2019-01-16 13:03:17'
            ),

            (object)array(
            'id' => 4509,
            'room_no' => 2,
            'room_type' => 'APARTMENT TWO BEDROOM',
            'meal_type' => 'TWO ROOMS',
            'updated_on' => '2019-01-16 13:03:09'
            ),

            (object)array(
            'id' => 4510,
            'room_no' => 3,
            'room_type' => 'APARTMENT ONE BEDROOM',
            'meal_type' => 'TWO ROOMS',
            'updated_on' => '2019-01-16 13:03:38'
            ),
            );

            $countedValues = array_count_values(
            array_map(function($x){
            return $x->meal_type;
            },$room_details)
            );

            //Build strings from value and display them.
            foreach( $countedValues as $mealtype=>$count ){
            echo $count . ' x ' . $mealtype . '<br />';
            }


            sample output:



            1 x ONE ROOM<br />
            2 x TWO ROOMS<br />


            you can try both examples on online PHP REPL



            Do similar for extracting room_type counts (can you figure it out?)



            NOTE if your php version does not support functions you can use create_function to dynamically create function to extract appropriate field from room Object or even better simply count the frequencies using a custom foreach loop (can you figure it out?)



            For example to simply count the frequencies without using array_count_values you can do something like the following:



            <?php

            $room_details = array(
            (object)array(
            'id' => 4508,
            'api' => 'hotelbeds',
            'room_no' => 1,
            'room_type' => 'APARTMENT ONE BEDROOM',
            'meal_type' => 'ONE ROOM',
            'updated_on' => '2019-01-16 13:03:17'
            ),

            (object)array(
            'id' => 4509,
            'room_no' => 2,
            'room_type' => 'APARTMENT TWO BEDROOM',
            'meal_type' => 'TWO ROOMS',
            'updated_on' => '2019-01-16 13:03:09'
            ),

            (object)array(
            'id' => 4510,
            'room_no' => 3,
            'room_type' => 'APARTMENT ONE BEDROOM',
            'meal_type' => 'TWO ROOMS',
            'updated_on' => '2019-01-16 13:03:38'
            ),
            );

            $freq = array();
            foreach($room_details as $roomObj) {
            $roomType = $roomObj->room_type;
            if ( !isset($freq[$roomType]) ) $freq[$roomType] = 1;
            else $freq[$roomType]++;
            }

            foreach($freq as $roomType=>$count) {
            echo $count . ' x ' . $roomType . '<br />';
            }





            share|improve this answer

































              0














              Simply you have to remove the S in room_type values and make a key without S e.g: $key = str_replace("S", "", $roomDetail['room_type']); and make a another array with result of count of the $key as follow:



              $result = ;
              foreach($room_details as $roomDetail) {
              $key = str_replace("S", "", $roomDetail['room_type']);
              echo $key;
              echo "n";
              $result[$key] += 1;
              }
              print_r($result);


              Code output:



              Array
              (
              [APARTMENT ONE BEDROOM] => 3
              [APARTMENT TWO BEDROOM] => 2
              [APARTMENT THREE BEDROOM] => 1
              )


              From this $result you can concatenate it as you like.






              share|improve this answer































                0














                A variation on the other answers....



                $room_details = array(
                (object)array(
                'id' => 4508,
                'api' => 'hotelbeds',
                'room_no' => 1,
                'room_type' => 'APARTMENT ONE BEDROOM',
                'meal_type' => 'ONE ROOM',
                'updated_on' => '2019-01-16 13:03:17'
                ),
                (object)array(
                'id' => 4509,
                'room_no' => 2,
                'room_type' => 'APARTMENT TWO BEDROOM',
                'meal_type' => 'TWO ROOMS',
                'updated_on' => '2019-01-16 13:03:09'
                ),
                (object)array(
                'id' => 4510,
                'room_no' => 3,
                'room_type' => 'APARTMENT ONE BEDROOMS',
                'meal_type' => 'TWO ROOMS',
                'updated_on' => '2019-01-16 13:03:38'
                ),
                (object)array(
                'id' => 4511,
                'room_no' => 3,
                'room_type' => 'APARTMENT TWO BEDROOMS',
                'meal_type' => 'TWO ROOMS',
                'updated_on' => '2019-01-16 13:03:38'
                ),
                (object)array(
                'id' => 4512,
                'room_no' => 3,
                'room_type' => 'APARTMENT THREE BEDROOMS',
                'meal_type' => 'THREE ROOMS',
                'updated_on' => '2019-01-16 13:03:38'
                ),
                (object)array(
                'id' => 4513,
                'room_no' => 3,
                'room_type' => 'APARTMENT ONE BEDROOMS',
                'meal_type' => 'ONE ROOM',
                'updated_on' => '2019-01-16 13:03:38'
                )
                );


                and to process the array



                $rooms=$meals=;
                foreach( $room_details as $index => $obj ){
                $room = rtrim( $obj->room_type,'S');
                $meal = rtrim( $obj->meal_type,'S');

                $rooms[ $room ]=empty( $rooms[ $room ] ) ? 1 : $rooms[ $room ] + 1;
                $meals[ $meal ]=empty( $meals[ $meal ] ) ? 1 : $meals[ $meal ] + 1;
                }


                foreach( $rooms as $key => $count )printf('%d x %s<br />',$count,$key);
                foreach( $meals as $key => $count )printf('%d x %s<br />',$count,$key);


                outputs:



                3 x APARTMENT ONE BEDROOM
                2 x APARTMENT TWO BEDROOM
                1 x APARTMENT THREE BEDROOM
                2 x ONE ROOM
                3 x TWO ROOM
                1 x THREE ROOM





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277269%2fhow-to-get-the-array-key-values-and-count-the-values-in-one-array-object%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  Hello Niranjan,



                  you can iterate through your array and count this data as followed,



                  <?php
                  $room_details = array(
                  array(
                  "id" => 4508,
                  "api" => "hotelbeds",
                  "room_no" => 1,
                  "room_type" => "APARTMENT ONE BEDROOMS",
                  "meal_type" => "ONE ROOM",
                  "updated_on" => "2019-01-16 13:03:17"
                  ),
                  array(
                  "id" => 4509,
                  "room_no" => 2,
                  "room_type" => "APARTMENT TWO BEDROOMS",
                  "meal_type" => "TWO ROOMS",
                  "updated_on" => "2019-01-16 13:03:09"
                  ),
                  array(
                  "id" => 4510,
                  "room_no" => 3,
                  "room_type" => "APARTMENT ONE BEDROOMS",
                  "meal_type" => "TWO ROOMS",
                  "updated_on" => "2019-01-16 13:03:38"
                  ),
                  array(
                  "id" => 4511,
                  "room_no" => 3,
                  "room_type" => "APARTMENT TWO BEDROOMS",
                  "meal_type" => "TWO ROOMS",
                  "updated_on" => "2019-01-16 13:03:38"
                  ),
                  array(
                  "id" => 4512,
                  "room_no" => 3,
                  "room_type" => "APARTMENT THREE BEDROOMS",
                  "meal_type" => "THREE ROOMS",
                  "updated_on" => "2019-01-16 13:03:38"
                  ),
                  array(
                  "id" => 4513,
                  "room_no" => 3,
                  "room_type" => "APARTMENT ONE BEDROOMS",
                  "meal_type" => "ONE ROOM",
                  "updated_on" => "2019-01-16 13:03:38"
                  )
                  );

                  $room_types = ;
                  $meal_types = ;

                  foreach($room_details as $k => $v) {
                  if(!array_key_exists($v["room_type"], $room_types)) {
                  $room_types[$v["room_type"]] = 1;
                  } else {
                  $room_types[$v["room_type"]] = $room_types[$v["room_type"]] + 1;
                  }

                  if(!array_key_exists($v["meal_type"], $meal_types)) {
                  $meal_types[$v["meal_type"]] = 1;
                  } else {
                  $meal_types[$v["meal_type"]] = $meal_types[$v["meal_type"]] + 1;
                  }
                  }

                  print_r($room_types);
                  print_r($meal_types);
                  ?>


                  Now you can access those arrays to fetch count.






                  share|improve this answer




























                    1














                    Hello Niranjan,



                    you can iterate through your array and count this data as followed,



                    <?php
                    $room_details = array(
                    array(
                    "id" => 4508,
                    "api" => "hotelbeds",
                    "room_no" => 1,
                    "room_type" => "APARTMENT ONE BEDROOMS",
                    "meal_type" => "ONE ROOM",
                    "updated_on" => "2019-01-16 13:03:17"
                    ),
                    array(
                    "id" => 4509,
                    "room_no" => 2,
                    "room_type" => "APARTMENT TWO BEDROOMS",
                    "meal_type" => "TWO ROOMS",
                    "updated_on" => "2019-01-16 13:03:09"
                    ),
                    array(
                    "id" => 4510,
                    "room_no" => 3,
                    "room_type" => "APARTMENT ONE BEDROOMS",
                    "meal_type" => "TWO ROOMS",
                    "updated_on" => "2019-01-16 13:03:38"
                    ),
                    array(
                    "id" => 4511,
                    "room_no" => 3,
                    "room_type" => "APARTMENT TWO BEDROOMS",
                    "meal_type" => "TWO ROOMS",
                    "updated_on" => "2019-01-16 13:03:38"
                    ),
                    array(
                    "id" => 4512,
                    "room_no" => 3,
                    "room_type" => "APARTMENT THREE BEDROOMS",
                    "meal_type" => "THREE ROOMS",
                    "updated_on" => "2019-01-16 13:03:38"
                    ),
                    array(
                    "id" => 4513,
                    "room_no" => 3,
                    "room_type" => "APARTMENT ONE BEDROOMS",
                    "meal_type" => "ONE ROOM",
                    "updated_on" => "2019-01-16 13:03:38"
                    )
                    );

                    $room_types = ;
                    $meal_types = ;

                    foreach($room_details as $k => $v) {
                    if(!array_key_exists($v["room_type"], $room_types)) {
                    $room_types[$v["room_type"]] = 1;
                    } else {
                    $room_types[$v["room_type"]] = $room_types[$v["room_type"]] + 1;
                    }

                    if(!array_key_exists($v["meal_type"], $meal_types)) {
                    $meal_types[$v["meal_type"]] = 1;
                    } else {
                    $meal_types[$v["meal_type"]] = $meal_types[$v["meal_type"]] + 1;
                    }
                    }

                    print_r($room_types);
                    print_r($meal_types);
                    ?>


                    Now you can access those arrays to fetch count.






                    share|improve this answer


























                      1












                      1








                      1







                      Hello Niranjan,



                      you can iterate through your array and count this data as followed,



                      <?php
                      $room_details = array(
                      array(
                      "id" => 4508,
                      "api" => "hotelbeds",
                      "room_no" => 1,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "ONE ROOM",
                      "updated_on" => "2019-01-16 13:03:17"
                      ),
                      array(
                      "id" => 4509,
                      "room_no" => 2,
                      "room_type" => "APARTMENT TWO BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:09"
                      ),
                      array(
                      "id" => 4510,
                      "room_no" => 3,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4511,
                      "room_no" => 3,
                      "room_type" => "APARTMENT TWO BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4512,
                      "room_no" => 3,
                      "room_type" => "APARTMENT THREE BEDROOMS",
                      "meal_type" => "THREE ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4513,
                      "room_no" => 3,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "ONE ROOM",
                      "updated_on" => "2019-01-16 13:03:38"
                      )
                      );

                      $room_types = ;
                      $meal_types = ;

                      foreach($room_details as $k => $v) {
                      if(!array_key_exists($v["room_type"], $room_types)) {
                      $room_types[$v["room_type"]] = 1;
                      } else {
                      $room_types[$v["room_type"]] = $room_types[$v["room_type"]] + 1;
                      }

                      if(!array_key_exists($v["meal_type"], $meal_types)) {
                      $meal_types[$v["meal_type"]] = 1;
                      } else {
                      $meal_types[$v["meal_type"]] = $meal_types[$v["meal_type"]] + 1;
                      }
                      }

                      print_r($room_types);
                      print_r($meal_types);
                      ?>


                      Now you can access those arrays to fetch count.






                      share|improve this answer













                      Hello Niranjan,



                      you can iterate through your array and count this data as followed,



                      <?php
                      $room_details = array(
                      array(
                      "id" => 4508,
                      "api" => "hotelbeds",
                      "room_no" => 1,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "ONE ROOM",
                      "updated_on" => "2019-01-16 13:03:17"
                      ),
                      array(
                      "id" => 4509,
                      "room_no" => 2,
                      "room_type" => "APARTMENT TWO BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:09"
                      ),
                      array(
                      "id" => 4510,
                      "room_no" => 3,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4511,
                      "room_no" => 3,
                      "room_type" => "APARTMENT TWO BEDROOMS",
                      "meal_type" => "TWO ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4512,
                      "room_no" => 3,
                      "room_type" => "APARTMENT THREE BEDROOMS",
                      "meal_type" => "THREE ROOMS",
                      "updated_on" => "2019-01-16 13:03:38"
                      ),
                      array(
                      "id" => 4513,
                      "room_no" => 3,
                      "room_type" => "APARTMENT ONE BEDROOMS",
                      "meal_type" => "ONE ROOM",
                      "updated_on" => "2019-01-16 13:03:38"
                      )
                      );

                      $room_types = ;
                      $meal_types = ;

                      foreach($room_details as $k => $v) {
                      if(!array_key_exists($v["room_type"], $room_types)) {
                      $room_types[$v["room_type"]] = 1;
                      } else {
                      $room_types[$v["room_type"]] = $room_types[$v["room_type"]] + 1;
                      }

                      if(!array_key_exists($v["meal_type"], $meal_types)) {
                      $meal_types[$v["meal_type"]] = 1;
                      } else {
                      $meal_types[$v["meal_type"]] = $meal_types[$v["meal_type"]] + 1;
                      }
                      }

                      print_r($room_types);
                      print_r($meal_types);
                      ?>


                      Now you can access those arrays to fetch count.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 20 at 14:27









                      Sebastian WaldbauerSebastian Waldbauer

                      365312




                      365312

























                          0














                          Try something like the following:



                          <?php

                          $room_details = array(
                          (object)array(
                          'id' => 4508,
                          'api' => 'hotelbeds',
                          'room_no' => 1,
                          'room_type' => 'APARTMENT ONE BEDROOM',
                          'meal_type' => 'ONE ROOM',
                          'updated_on' => '2019-01-16 13:03:17'
                          ),

                          (object)array(
                          'id' => 4509,
                          'room_no' => 2,
                          'room_type' => 'APARTMENT TWO BEDROOM',
                          'meal_type' => 'TWO ROOMS',
                          'updated_on' => '2019-01-16 13:03:09'
                          ),

                          (object)array(
                          'id' => 4510,
                          'room_no' => 3,
                          'room_type' => 'APARTMENT ONE BEDROOM',
                          'meal_type' => 'TWO ROOMS',
                          'updated_on' => '2019-01-16 13:03:38'
                          ),
                          );

                          $countedValues = array_count_values(
                          array_map(function($x){
                          return $x->meal_type;
                          },$room_details)
                          );

                          //Build strings from value and display them.
                          foreach( $countedValues as $mealtype=>$count ){
                          echo $count . ' x ' . $mealtype . '<br />';
                          }


                          sample output:



                          1 x ONE ROOM<br />
                          2 x TWO ROOMS<br />


                          you can try both examples on online PHP REPL



                          Do similar for extracting room_type counts (can you figure it out?)



                          NOTE if your php version does not support functions you can use create_function to dynamically create function to extract appropriate field from room Object or even better simply count the frequencies using a custom foreach loop (can you figure it out?)



                          For example to simply count the frequencies without using array_count_values you can do something like the following:



                          <?php

                          $room_details = array(
                          (object)array(
                          'id' => 4508,
                          'api' => 'hotelbeds',
                          'room_no' => 1,
                          'room_type' => 'APARTMENT ONE BEDROOM',
                          'meal_type' => 'ONE ROOM',
                          'updated_on' => '2019-01-16 13:03:17'
                          ),

                          (object)array(
                          'id' => 4509,
                          'room_no' => 2,
                          'room_type' => 'APARTMENT TWO BEDROOM',
                          'meal_type' => 'TWO ROOMS',
                          'updated_on' => '2019-01-16 13:03:09'
                          ),

                          (object)array(
                          'id' => 4510,
                          'room_no' => 3,
                          'room_type' => 'APARTMENT ONE BEDROOM',
                          'meal_type' => 'TWO ROOMS',
                          'updated_on' => '2019-01-16 13:03:38'
                          ),
                          );

                          $freq = array();
                          foreach($room_details as $roomObj) {
                          $roomType = $roomObj->room_type;
                          if ( !isset($freq[$roomType]) ) $freq[$roomType] = 1;
                          else $freq[$roomType]++;
                          }

                          foreach($freq as $roomType=>$count) {
                          echo $count . ' x ' . $roomType . '<br />';
                          }





                          share|improve this answer






























                            0














                            Try something like the following:



                            <?php

                            $room_details = array(
                            (object)array(
                            'id' => 4508,
                            'api' => 'hotelbeds',
                            'room_no' => 1,
                            'room_type' => 'APARTMENT ONE BEDROOM',
                            'meal_type' => 'ONE ROOM',
                            'updated_on' => '2019-01-16 13:03:17'
                            ),

                            (object)array(
                            'id' => 4509,
                            'room_no' => 2,
                            'room_type' => 'APARTMENT TWO BEDROOM',
                            'meal_type' => 'TWO ROOMS',
                            'updated_on' => '2019-01-16 13:03:09'
                            ),

                            (object)array(
                            'id' => 4510,
                            'room_no' => 3,
                            'room_type' => 'APARTMENT ONE BEDROOM',
                            'meal_type' => 'TWO ROOMS',
                            'updated_on' => '2019-01-16 13:03:38'
                            ),
                            );

                            $countedValues = array_count_values(
                            array_map(function($x){
                            return $x->meal_type;
                            },$room_details)
                            );

                            //Build strings from value and display them.
                            foreach( $countedValues as $mealtype=>$count ){
                            echo $count . ' x ' . $mealtype . '<br />';
                            }


                            sample output:



                            1 x ONE ROOM<br />
                            2 x TWO ROOMS<br />


                            you can try both examples on online PHP REPL



                            Do similar for extracting room_type counts (can you figure it out?)



                            NOTE if your php version does not support functions you can use create_function to dynamically create function to extract appropriate field from room Object or even better simply count the frequencies using a custom foreach loop (can you figure it out?)



                            For example to simply count the frequencies without using array_count_values you can do something like the following:



                            <?php

                            $room_details = array(
                            (object)array(
                            'id' => 4508,
                            'api' => 'hotelbeds',
                            'room_no' => 1,
                            'room_type' => 'APARTMENT ONE BEDROOM',
                            'meal_type' => 'ONE ROOM',
                            'updated_on' => '2019-01-16 13:03:17'
                            ),

                            (object)array(
                            'id' => 4509,
                            'room_no' => 2,
                            'room_type' => 'APARTMENT TWO BEDROOM',
                            'meal_type' => 'TWO ROOMS',
                            'updated_on' => '2019-01-16 13:03:09'
                            ),

                            (object)array(
                            'id' => 4510,
                            'room_no' => 3,
                            'room_type' => 'APARTMENT ONE BEDROOM',
                            'meal_type' => 'TWO ROOMS',
                            'updated_on' => '2019-01-16 13:03:38'
                            ),
                            );

                            $freq = array();
                            foreach($room_details as $roomObj) {
                            $roomType = $roomObj->room_type;
                            if ( !isset($freq[$roomType]) ) $freq[$roomType] = 1;
                            else $freq[$roomType]++;
                            }

                            foreach($freq as $roomType=>$count) {
                            echo $count . ' x ' . $roomType . '<br />';
                            }





                            share|improve this answer




























                              0












                              0








                              0







                              Try something like the following:



                              <?php

                              $room_details = array(
                              (object)array(
                              'id' => 4508,
                              'api' => 'hotelbeds',
                              'room_no' => 1,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'ONE ROOM',
                              'updated_on' => '2019-01-16 13:03:17'
                              ),

                              (object)array(
                              'id' => 4509,
                              'room_no' => 2,
                              'room_type' => 'APARTMENT TWO BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:09'
                              ),

                              (object)array(
                              'id' => 4510,
                              'room_no' => 3,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:38'
                              ),
                              );

                              $countedValues = array_count_values(
                              array_map(function($x){
                              return $x->meal_type;
                              },$room_details)
                              );

                              //Build strings from value and display them.
                              foreach( $countedValues as $mealtype=>$count ){
                              echo $count . ' x ' . $mealtype . '<br />';
                              }


                              sample output:



                              1 x ONE ROOM<br />
                              2 x TWO ROOMS<br />


                              you can try both examples on online PHP REPL



                              Do similar for extracting room_type counts (can you figure it out?)



                              NOTE if your php version does not support functions you can use create_function to dynamically create function to extract appropriate field from room Object or even better simply count the frequencies using a custom foreach loop (can you figure it out?)



                              For example to simply count the frequencies without using array_count_values you can do something like the following:



                              <?php

                              $room_details = array(
                              (object)array(
                              'id' => 4508,
                              'api' => 'hotelbeds',
                              'room_no' => 1,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'ONE ROOM',
                              'updated_on' => '2019-01-16 13:03:17'
                              ),

                              (object)array(
                              'id' => 4509,
                              'room_no' => 2,
                              'room_type' => 'APARTMENT TWO BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:09'
                              ),

                              (object)array(
                              'id' => 4510,
                              'room_no' => 3,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:38'
                              ),
                              );

                              $freq = array();
                              foreach($room_details as $roomObj) {
                              $roomType = $roomObj->room_type;
                              if ( !isset($freq[$roomType]) ) $freq[$roomType] = 1;
                              else $freq[$roomType]++;
                              }

                              foreach($freq as $roomType=>$count) {
                              echo $count . ' x ' . $roomType . '<br />';
                              }





                              share|improve this answer















                              Try something like the following:



                              <?php

                              $room_details = array(
                              (object)array(
                              'id' => 4508,
                              'api' => 'hotelbeds',
                              'room_no' => 1,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'ONE ROOM',
                              'updated_on' => '2019-01-16 13:03:17'
                              ),

                              (object)array(
                              'id' => 4509,
                              'room_no' => 2,
                              'room_type' => 'APARTMENT TWO BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:09'
                              ),

                              (object)array(
                              'id' => 4510,
                              'room_no' => 3,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:38'
                              ),
                              );

                              $countedValues = array_count_values(
                              array_map(function($x){
                              return $x->meal_type;
                              },$room_details)
                              );

                              //Build strings from value and display them.
                              foreach( $countedValues as $mealtype=>$count ){
                              echo $count . ' x ' . $mealtype . '<br />';
                              }


                              sample output:



                              1 x ONE ROOM<br />
                              2 x TWO ROOMS<br />


                              you can try both examples on online PHP REPL



                              Do similar for extracting room_type counts (can you figure it out?)



                              NOTE if your php version does not support functions you can use create_function to dynamically create function to extract appropriate field from room Object or even better simply count the frequencies using a custom foreach loop (can you figure it out?)



                              For example to simply count the frequencies without using array_count_values you can do something like the following:



                              <?php

                              $room_details = array(
                              (object)array(
                              'id' => 4508,
                              'api' => 'hotelbeds',
                              'room_no' => 1,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'ONE ROOM',
                              'updated_on' => '2019-01-16 13:03:17'
                              ),

                              (object)array(
                              'id' => 4509,
                              'room_no' => 2,
                              'room_type' => 'APARTMENT TWO BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:09'
                              ),

                              (object)array(
                              'id' => 4510,
                              'room_no' => 3,
                              'room_type' => 'APARTMENT ONE BEDROOM',
                              'meal_type' => 'TWO ROOMS',
                              'updated_on' => '2019-01-16 13:03:38'
                              ),
                              );

                              $freq = array();
                              foreach($room_details as $roomObj) {
                              $roomType = $roomObj->room_type;
                              if ( !isset($freq[$roomType]) ) $freq[$roomType] = 1;
                              else $freq[$roomType]++;
                              }

                              foreach($freq as $roomType=>$count) {
                              echo $count . ' x ' . $roomType . '<br />';
                              }






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 20 at 14:29

























                              answered Jan 20 at 14:23









                              Nikos M.Nikos M.

                              4,50521824




                              4,50521824























                                  0














                                  Simply you have to remove the S in room_type values and make a key without S e.g: $key = str_replace("S", "", $roomDetail['room_type']); and make a another array with result of count of the $key as follow:



                                  $result = ;
                                  foreach($room_details as $roomDetail) {
                                  $key = str_replace("S", "", $roomDetail['room_type']);
                                  echo $key;
                                  echo "n";
                                  $result[$key] += 1;
                                  }
                                  print_r($result);


                                  Code output:



                                  Array
                                  (
                                  [APARTMENT ONE BEDROOM] => 3
                                  [APARTMENT TWO BEDROOM] => 2
                                  [APARTMENT THREE BEDROOM] => 1
                                  )


                                  From this $result you can concatenate it as you like.






                                  share|improve this answer




























                                    0














                                    Simply you have to remove the S in room_type values and make a key without S e.g: $key = str_replace("S", "", $roomDetail['room_type']); and make a another array with result of count of the $key as follow:



                                    $result = ;
                                    foreach($room_details as $roomDetail) {
                                    $key = str_replace("S", "", $roomDetail['room_type']);
                                    echo $key;
                                    echo "n";
                                    $result[$key] += 1;
                                    }
                                    print_r($result);


                                    Code output:



                                    Array
                                    (
                                    [APARTMENT ONE BEDROOM] => 3
                                    [APARTMENT TWO BEDROOM] => 2
                                    [APARTMENT THREE BEDROOM] => 1
                                    )


                                    From this $result you can concatenate it as you like.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Simply you have to remove the S in room_type values and make a key without S e.g: $key = str_replace("S", "", $roomDetail['room_type']); and make a another array with result of count of the $key as follow:



                                      $result = ;
                                      foreach($room_details as $roomDetail) {
                                      $key = str_replace("S", "", $roomDetail['room_type']);
                                      echo $key;
                                      echo "n";
                                      $result[$key] += 1;
                                      }
                                      print_r($result);


                                      Code output:



                                      Array
                                      (
                                      [APARTMENT ONE BEDROOM] => 3
                                      [APARTMENT TWO BEDROOM] => 2
                                      [APARTMENT THREE BEDROOM] => 1
                                      )


                                      From this $result you can concatenate it as you like.






                                      share|improve this answer













                                      Simply you have to remove the S in room_type values and make a key without S e.g: $key = str_replace("S", "", $roomDetail['room_type']); and make a another array with result of count of the $key as follow:



                                      $result = ;
                                      foreach($room_details as $roomDetail) {
                                      $key = str_replace("S", "", $roomDetail['room_type']);
                                      echo $key;
                                      echo "n";
                                      $result[$key] += 1;
                                      }
                                      print_r($result);


                                      Code output:



                                      Array
                                      (
                                      [APARTMENT ONE BEDROOM] => 3
                                      [APARTMENT TWO BEDROOM] => 2
                                      [APARTMENT THREE BEDROOM] => 1
                                      )


                                      From this $result you can concatenate it as you like.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 20 at 14:32









                                      krishna Prasadkrishna Prasad

                                      1,09411221




                                      1,09411221























                                          0














                                          A variation on the other answers....



                                          $room_details = array(
                                          (object)array(
                                          'id' => 4508,
                                          'api' => 'hotelbeds',
                                          'room_no' => 1,
                                          'room_type' => 'APARTMENT ONE BEDROOM',
                                          'meal_type' => 'ONE ROOM',
                                          'updated_on' => '2019-01-16 13:03:17'
                                          ),
                                          (object)array(
                                          'id' => 4509,
                                          'room_no' => 2,
                                          'room_type' => 'APARTMENT TWO BEDROOM',
                                          'meal_type' => 'TWO ROOMS',
                                          'updated_on' => '2019-01-16 13:03:09'
                                          ),
                                          (object)array(
                                          'id' => 4510,
                                          'room_no' => 3,
                                          'room_type' => 'APARTMENT ONE BEDROOMS',
                                          'meal_type' => 'TWO ROOMS',
                                          'updated_on' => '2019-01-16 13:03:38'
                                          ),
                                          (object)array(
                                          'id' => 4511,
                                          'room_no' => 3,
                                          'room_type' => 'APARTMENT TWO BEDROOMS',
                                          'meal_type' => 'TWO ROOMS',
                                          'updated_on' => '2019-01-16 13:03:38'
                                          ),
                                          (object)array(
                                          'id' => 4512,
                                          'room_no' => 3,
                                          'room_type' => 'APARTMENT THREE BEDROOMS',
                                          'meal_type' => 'THREE ROOMS',
                                          'updated_on' => '2019-01-16 13:03:38'
                                          ),
                                          (object)array(
                                          'id' => 4513,
                                          'room_no' => 3,
                                          'room_type' => 'APARTMENT ONE BEDROOMS',
                                          'meal_type' => 'ONE ROOM',
                                          'updated_on' => '2019-01-16 13:03:38'
                                          )
                                          );


                                          and to process the array



                                          $rooms=$meals=;
                                          foreach( $room_details as $index => $obj ){
                                          $room = rtrim( $obj->room_type,'S');
                                          $meal = rtrim( $obj->meal_type,'S');

                                          $rooms[ $room ]=empty( $rooms[ $room ] ) ? 1 : $rooms[ $room ] + 1;
                                          $meals[ $meal ]=empty( $meals[ $meal ] ) ? 1 : $meals[ $meal ] + 1;
                                          }


                                          foreach( $rooms as $key => $count )printf('%d x %s<br />',$count,$key);
                                          foreach( $meals as $key => $count )printf('%d x %s<br />',$count,$key);


                                          outputs:



                                          3 x APARTMENT ONE BEDROOM
                                          2 x APARTMENT TWO BEDROOM
                                          1 x APARTMENT THREE BEDROOM
                                          2 x ONE ROOM
                                          3 x TWO ROOM
                                          1 x THREE ROOM





                                          share|improve this answer




























                                            0














                                            A variation on the other answers....



                                            $room_details = array(
                                            (object)array(
                                            'id' => 4508,
                                            'api' => 'hotelbeds',
                                            'room_no' => 1,
                                            'room_type' => 'APARTMENT ONE BEDROOM',
                                            'meal_type' => 'ONE ROOM',
                                            'updated_on' => '2019-01-16 13:03:17'
                                            ),
                                            (object)array(
                                            'id' => 4509,
                                            'room_no' => 2,
                                            'room_type' => 'APARTMENT TWO BEDROOM',
                                            'meal_type' => 'TWO ROOMS',
                                            'updated_on' => '2019-01-16 13:03:09'
                                            ),
                                            (object)array(
                                            'id' => 4510,
                                            'room_no' => 3,
                                            'room_type' => 'APARTMENT ONE BEDROOMS',
                                            'meal_type' => 'TWO ROOMS',
                                            'updated_on' => '2019-01-16 13:03:38'
                                            ),
                                            (object)array(
                                            'id' => 4511,
                                            'room_no' => 3,
                                            'room_type' => 'APARTMENT TWO BEDROOMS',
                                            'meal_type' => 'TWO ROOMS',
                                            'updated_on' => '2019-01-16 13:03:38'
                                            ),
                                            (object)array(
                                            'id' => 4512,
                                            'room_no' => 3,
                                            'room_type' => 'APARTMENT THREE BEDROOMS',
                                            'meal_type' => 'THREE ROOMS',
                                            'updated_on' => '2019-01-16 13:03:38'
                                            ),
                                            (object)array(
                                            'id' => 4513,
                                            'room_no' => 3,
                                            'room_type' => 'APARTMENT ONE BEDROOMS',
                                            'meal_type' => 'ONE ROOM',
                                            'updated_on' => '2019-01-16 13:03:38'
                                            )
                                            );


                                            and to process the array



                                            $rooms=$meals=;
                                            foreach( $room_details as $index => $obj ){
                                            $room = rtrim( $obj->room_type,'S');
                                            $meal = rtrim( $obj->meal_type,'S');

                                            $rooms[ $room ]=empty( $rooms[ $room ] ) ? 1 : $rooms[ $room ] + 1;
                                            $meals[ $meal ]=empty( $meals[ $meal ] ) ? 1 : $meals[ $meal ] + 1;
                                            }


                                            foreach( $rooms as $key => $count )printf('%d x %s<br />',$count,$key);
                                            foreach( $meals as $key => $count )printf('%d x %s<br />',$count,$key);


                                            outputs:



                                            3 x APARTMENT ONE BEDROOM
                                            2 x APARTMENT TWO BEDROOM
                                            1 x APARTMENT THREE BEDROOM
                                            2 x ONE ROOM
                                            3 x TWO ROOM
                                            1 x THREE ROOM





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              A variation on the other answers....



                                              $room_details = array(
                                              (object)array(
                                              'id' => 4508,
                                              'api' => 'hotelbeds',
                                              'room_no' => 1,
                                              'room_type' => 'APARTMENT ONE BEDROOM',
                                              'meal_type' => 'ONE ROOM',
                                              'updated_on' => '2019-01-16 13:03:17'
                                              ),
                                              (object)array(
                                              'id' => 4509,
                                              'room_no' => 2,
                                              'room_type' => 'APARTMENT TWO BEDROOM',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:09'
                                              ),
                                              (object)array(
                                              'id' => 4510,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT ONE BEDROOMS',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4511,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT TWO BEDROOMS',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4512,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT THREE BEDROOMS',
                                              'meal_type' => 'THREE ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4513,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT ONE BEDROOMS',
                                              'meal_type' => 'ONE ROOM',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              )
                                              );


                                              and to process the array



                                              $rooms=$meals=;
                                              foreach( $room_details as $index => $obj ){
                                              $room = rtrim( $obj->room_type,'S');
                                              $meal = rtrim( $obj->meal_type,'S');

                                              $rooms[ $room ]=empty( $rooms[ $room ] ) ? 1 : $rooms[ $room ] + 1;
                                              $meals[ $meal ]=empty( $meals[ $meal ] ) ? 1 : $meals[ $meal ] + 1;
                                              }


                                              foreach( $rooms as $key => $count )printf('%d x %s<br />',$count,$key);
                                              foreach( $meals as $key => $count )printf('%d x %s<br />',$count,$key);


                                              outputs:



                                              3 x APARTMENT ONE BEDROOM
                                              2 x APARTMENT TWO BEDROOM
                                              1 x APARTMENT THREE BEDROOM
                                              2 x ONE ROOM
                                              3 x TWO ROOM
                                              1 x THREE ROOM





                                              share|improve this answer













                                              A variation on the other answers....



                                              $room_details = array(
                                              (object)array(
                                              'id' => 4508,
                                              'api' => 'hotelbeds',
                                              'room_no' => 1,
                                              'room_type' => 'APARTMENT ONE BEDROOM',
                                              'meal_type' => 'ONE ROOM',
                                              'updated_on' => '2019-01-16 13:03:17'
                                              ),
                                              (object)array(
                                              'id' => 4509,
                                              'room_no' => 2,
                                              'room_type' => 'APARTMENT TWO BEDROOM',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:09'
                                              ),
                                              (object)array(
                                              'id' => 4510,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT ONE BEDROOMS',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4511,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT TWO BEDROOMS',
                                              'meal_type' => 'TWO ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4512,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT THREE BEDROOMS',
                                              'meal_type' => 'THREE ROOMS',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              ),
                                              (object)array(
                                              'id' => 4513,
                                              'room_no' => 3,
                                              'room_type' => 'APARTMENT ONE BEDROOMS',
                                              'meal_type' => 'ONE ROOM',
                                              'updated_on' => '2019-01-16 13:03:38'
                                              )
                                              );


                                              and to process the array



                                              $rooms=$meals=;
                                              foreach( $room_details as $index => $obj ){
                                              $room = rtrim( $obj->room_type,'S');
                                              $meal = rtrim( $obj->meal_type,'S');

                                              $rooms[ $room ]=empty( $rooms[ $room ] ) ? 1 : $rooms[ $room ] + 1;
                                              $meals[ $meal ]=empty( $meals[ $meal ] ) ? 1 : $meals[ $meal ] + 1;
                                              }


                                              foreach( $rooms as $key => $count )printf('%d x %s<br />',$count,$key);
                                              foreach( $meals as $key => $count )printf('%d x %s<br />',$count,$key);


                                              outputs:



                                              3 x APARTMENT ONE BEDROOM
                                              2 x APARTMENT TWO BEDROOM
                                              1 x APARTMENT THREE BEDROOM
                                              2 x ONE ROOM
                                              3 x TWO ROOM
                                              1 x THREE ROOM






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jan 20 at 14:34









                                              RamRaiderRamRaider

                                              17.8k31834




                                              17.8k31834






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277269%2fhow-to-get-the-array-key-values-and-count-the-values-in-one-array-object%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  How fix org.hibernate.TransientPropertyValueException

                                                  Updating UILabel text programmatically using a function

                                                  Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage