如何获取数组键值并计算一个数组对象中的值

时间:2019-01-20 14:07:51

标签: php arrays

我怀疑在php数组中获取数组键值并计算在php数组中重复的相同键。 这是通过一个变量发布我的数组对象输出,以获取此数据。 这里我的要求是在这个数组中,我有[room_type]和[meal_type]。

  

这里[room_type] =>公寓1卧室是3次来过这里   数组。所以我需要的结果是3 x公寓一间卧室   [room_type] =>此公寓有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
        )
)

我想在One div标签中显示此结果。

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

然后[meal_type] =>一个ROOM进入该数组2次,因此结果是2 x一个ROOM [meal_type] =>两个ROOM进入此数组3次,因此结果为3 x两个ROOM [meal_type] =>三个房间进入此数组1次,因此结果为1 x三个房间 我想在“另一个Div标签”中显示此结果。

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

4 个答案:

答案 0 :(得分:1)

你好Niranjan

您可以遍历数组并按如下方式计数此数据,

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

现在您可以访问这些数组以获取计数。

答案 1 :(得分:0)

尝试以下操作:

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

您可以在online PHP REPL

上尝试两个示例

提取room_type计数是否类似(您能找出答案吗?)

注意,如果您的php版本不支持functions,则可以使用create_function动态创建函数以从room对象中提取适当的字段,甚至更好使用自定义的foreach循环来计算频率(您能找出答案吗?)

例如,仅使用array_count_values即可计算频率,您可以执行以下操作:

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

答案 2 :(得分:0)

仅需删除room_type值中的S并创建一个不带S的键,例如:$key = str_replace("S", "", $roomDetail['room_type']);并创建另一个数组,其数组的$ key计数结果如下:

$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,您可以根据需要连接它。

答案 3 :(得分:0)

其他答案的变体......

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

并处理数组

$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