PHP多维数组按值搜索,然后返回值之和

PHP多维数组按值搜索,然后返回值之和,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,假设我们有两个多维数组 $hotel = array( array( 'name' => 'hotel one', 'hotel_price' => '100' 'hotel_child_price' => '50' ), array( 'name' => 'hotel two', 'hotel_price' => '200' 'hotel_child_price' => '100' ), array(

假设我们有两个多维数组

$hotel = array(
array(
    'name' => 'hotel one',
    'hotel_price' => '100'
    'hotel_child_price' => '50'
),
array(
    'name' => 'hotel two',
    'hotel_price' => '200'
    'hotel_child_price' => '100'
),
array(
    'name' => 'hotel three',
    'hotel_price' => '300'
    'hotel_child_price' => '200'
)
);

$user_selected_hotel = array(
array(
    'name' => 'hotel one',
    'hotel_price' => '100'
),
array(
    'name' => 'hotel three',
    'hotel_price' => '300'
)
);
如何从第一个数组中检索用户选择的hotel_child_价格,即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;
} 

我已经更正了源数据(缺少一些
),但主要是关于如何找到每条记录

我已经更改了
searchForId()
方法来搜索记录(使用一个简单的
foreach()
,找到后,它返回该名称的数组元素。当它返回数组元素时,它所做的就是提取
“hotel\u child\u 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;
} 
您可以使用来轻松实现所需的结果,而无需
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;
更简短的回答(代码行):


<强>注释< /强>:您的一些数据缺少“代码>,<代码>。请在运行上述代码段之前考虑修改您的示例数据。

< P>我不认为我完全理解您想要什么。但是请检查以下是您正在搜索的内容,请:

$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”之和


让我知道这是否是您想要的。

请回答您的问题,并包括您尝试过但未按预期工作的内容。这意味着用户选择了200+50吗?我不明白……我经常使用数组函数,它们非常有用。但我也意识到它们通常可以做比需要更多的工作。用于考试ple使用
数组_列()
将从所有行中提取数据,即使您只搜索了前几行。@NigelRen感谢您的评论,我不知道
数组_列
造成的开销。我将尝试改进我的答案。但实现的逻辑是,首先需要从
$hotel
中提取所有值,然后过滤它们根据
$user_selected_hotel
的值,为了得到最终的价格并求和它们。这不是为了改进你的答案-答案很好,只是为了了解你使用的代码以及它在后台的作用。就这样,我连续两天不睡觉地敲打我的头得到了回报,你得到了回报我已经解决了这个难题,非常感谢
$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;
}