php - 在一個 array 對象中,如何獲取 array 鍵值並計算值

  显示原文与译文双语对照的内容
123 1

php數組中的array key-value 並計算在 php array 中重複的鍵。 這裡發布了我的array 對象輸出通過一個變數我得到這個數據。 我的要求是在這個 array 中我有 [room_type] 和 [meal_type] 。

這裡 [room_type] => 公寓一個卧室是 3次的這個 array 。 所以我需要的結果是 3個房間,在這裡 [room_type] => 公寓兩個卧室是這個 array的2次。 所以我需要的結果是 2 x 公寓兩個卧室 [room_type] => 房間三個卧室是 1個時間。 所以我需要的結果是 1 x 公寓三卧室


$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


 )


)



這個結果我想在一個div標籤中顯示,。


<div class="roomtype">


<p>


3 x APARTMENT ONE BEDROOM


2 x APARTMENT TWO BEDROOM


1 x APARTMENT THREE BEDROOM


</p>


</div>



結果就是,這兩個房間都有一個房間,所以結果是 1房間,這樣結果是房間,這樣結果是房間。


<div class="mealtype">


<p>


2 x ONE ROOM 


3 x TWO ROOMS


1 x THREE ROOMS


</p>


</div>



我嘗試下面的代碼無法工作,可以請任何人幫忙。


 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/>';


 }



时间: 原作者:

92 1

你好 Niranjan

你可以迭代 array 並按如下方式計數這些數據。


<?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);


?>



現在你可以訪問這些數組以獲取計數。

84 4

嘗試以下類似的內容:


<?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/>';


}



示例輸出:


1 x ONE ROOM<br/>


2 x TWO ROOMS<br/>



你可以在在線 PHP REPL中同時嘗試這兩個示例。

room_type 計數進行類似的提取( 你能找出它)?

注意如果php版本不支持 functions,你可以使用 create_function 動態創建函數,從 room 對象中提取相應的欄位,或者更簡單地計算出使用自定義的foreach 循環的頻率( 你能找到)?

例如你可以使用以下方法在不使用的情況下計算頻率為的:


<?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/>';


}



原作者:
99 5

只需刪除room_type值中的S,並在沒有 S 比如的情況下生成密鑰: $key = str_replace("S","", $roomDetail['room_type']); 然後使用 $key的計數生成另一個 array,如下所示:


$result = [];


foreach($room_details as $roomDetail) {


 $key = str_replace("S","", $roomDetail['room_type']);


 echo $key;


 echo"n";


 $result[$key] += 1;


}


print_r($result);



代碼輸出:


Array


(


 [APARTMENT ONE BEDROOM] => 3


 [APARTMENT TWO BEDROOM] => 2


 [APARTMENT THREE BEDROOM] => 1


)



從這個 $result 中,你可以根據需要連接它。

105 4

其他答案的變化。


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


 )


);



並處理 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);



輸出:


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



...