通过另一个二维数组中的共享列过滤二维数组,然后对另一列中的值求和

问题描述 投票:0回答:4

如何通过另一个数组中的共享列值过滤二维数组,然后对过滤数据集的另一列中的值求和?

$hotel = [
    [
        'name' => 'hotel one',
        'hotel_price' => '100',
        'hotel_child_price' => '50'
    ],
    [
        'name' => 'hotel two',
        'hotel_price' => '200',
        'hotel_child_price' => '100'
    ],
    [
        'name' => 'hotel three',
        'hotel_price' => '300',
        'hotel_child_price' => '200'
    ],
];

$user_selected_hotel = [
    [
        'name' => 'hotel one',
        'hotel_price' => '100'
    ],
    [
        'name' => 'hotel three',
        'hotel_price' => '300'
    ]
];

如何从第一个数组中检索用户选择的

hotel_child_price
,即
50
200
=
250
之和。

第二个数组代表用户的酒店选择。

我当前的代码:

foreach ( $user_selected_hotel as $lp => $hp) {
    $hotl_slct = $hp;
}

function searchForId($hotl_slct, $hotel) {
    foreach ($hotel as $cust1 => $cust_htl) {
        if ($cust_htl['name'] === $hotl_slct) {
            return $cust1;
        }
    }
    return null;
} 
php arrays multidimensional-array sum intersection
4个回答
1
投票

您可以使用数组函数轻松实现您想要的结果,而无需

for
循环:

// Get hotel prices.
$prices = array_column($user_selected_hotel, 'hotel_price');
// Find those records from $hotel whose hotel prices are in prices.
$resultPrices = array_filter($hotel, function($elem) use ($prices) { return in_array($elem['hotel_price'], $prices);});
// Sum them.
$finalResult = array_sum(array_column($resultPrices, 'hotel_child_price'));
// Print result.
echo $finalResult;

更短的答案(以代码行表示):

$resultPrices = array_filter($hotel, function($elem) use ($user_selected_hotel) { return in_array($elem['hotel_price'], array_column($user_selected_hotel, 'hotel_price'));});
$finalResult = array_sum(array_column($resultPrices, 'hotel_child_price'));

注意:您的一些数据丢失了

,
。在运行上述代码片段之前,请考虑更正您的示例数据。


1
投票

我已经更正了源数据(漏掉了一些

,
),但主要是如何找到每条记录。

我更改了

searchForId()
方法来搜索记录(使用简单的
foreach()
,找到后,它返回该名称的数组元素。当它返回数组元素时,它所做的就是提取
'hotel_child_price'
值并将其添加到运行总计中。

$total = 0;
foreach( $user_selected_hotel as $lp => $hp) {
    $total += searchForID($hp['name'], $hotel)['hotel_child_price'];
}
echo $total;

function searchForId($hotl_slct, $hotels ) {
    foreach ( $hotels as $hotel )   {
        if ( $hotel['name'] == $hotl_slct ) {
            return $hotel;
        }
    }
    return null;
} 

0
投票

我认为我没有完全理解你想要什么。但请检查以下内容是否是您要搜索的内容:

$hotel = [
    [
        'name' => 'hotel one',
        'hotel_price' => '100',
        'hotel_child_price' => '50'
    ],
    [
        'name' => 'hotel two',
        'hotel_price' => '200',
        'hotel_child_price' => '100'
    ],
    [
        'name' => 'hotel three',
        'hotel_price' => '300',
        'hotel_child_price' => '200'
    ],
];

$user_selected_hotel = [
    [
        'name' => 'hotel one',
        'hotel_price' => '100'
    ],
    [
        'name' => 'hotel three',
        'hotel_price' => '300'
    ]
];

foreach($user_selected_hotel as $selected) {
    $key = array_search($selected['name'], array_column($hotel, 'name'));
    $merge = array_merge($selected, $hotel[$key]);
    $merge['hotel_price'] = $selected['hotel_price'] + $hotel[$key]['hotel_price'];
    return $merge;
}

线

$merge['hotel_price'] = $selected['hotel_price'] + $hotel[$key]['hotel_price'];
代表 $hotel 和 $user_selected_hotel 数组中的“hotel_price”之间的总和。

请告诉我这是否是您想要的。


0
投票

函数式基于值的过滤:(演示

var_export(
    array_sum(
        array_column(
            array_uintersect(
                $hotel,
                $user_selected_hotel,
                fn($a, $b) => $a['name'] <=> $b['name']
            ),
            'hotel_child_price'
        )
    )
);

函数式基于键的过滤:(演示

var_export(
    array_sum(
        array_intersect_key(
            array_column($hotel, 'hotel_child_price', 'name'),
            array_column($user_selected_hotel, null, 'name')
        )
    )
);

具有选择性行解构和查找数组的 Foreach 循环:(演示

$lookup = array_column($hotel, 'hotel_child_price', 'name');
$sum = 0;
foreach ($user_selected_hotel as ['name' => $name]) {
    $sum += $lookup[$name];
}
var_export($sum);
© www.soinside.com 2019 - 2024. All rights reserved.