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



...