Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查多个字符串的值,并返回带值的字符串_Php_Arrays_Sorting - Fatal编程技术网

Php 检查多个字符串的值,并返回带值的字符串

Php 检查多个字符串的值,并返回带值的字符串,php,arrays,sorting,Php,Arrays,Sorting,我有以下代码: preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout); preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night); //price per night preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total); //booking

我有以下代码:

preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout);
preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night); //price per night
preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total); //booking price
这3个值中只有1个有值。我可以写什么函数来检查:

$airbnb_total_payout[1]
$airbnb_per_night[1] 
$booking_total[1]

哪一个有价值?并且只返回它。谢谢

您可以尝试一系列if语句:

function getValue($comments,$airbnb_total_payout,$airbnb_per_night,$booking_total) {
     if(preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout)) return $airbnb_total_payout[1];
     if(preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night)) return $airbnb_per_night[1];
     if(preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total)) return $booking_total[1];
     return false; // Return value if none of the conditions are met above.
}
要调用函数,请使用:

$myValueResult = getValue($comments,$airbnb_total_payout,$airbnb_per_night,$booking_total);

$myValueResult将具有函数的结果值。

您可以尝试一系列if语句:

function getValue($comments,$airbnb_total_payout,$airbnb_per_night,$booking_total) {
     if(preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout)) return $airbnb_total_payout[1];
     if(preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night)) return $airbnb_per_night[1];
     if(preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total)) return $booking_total[1];
     return false; // Return value if none of the conditions are met above.
}
function getValue ($comments) {
    preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout); 
     if (strlen( $airbnb_total_payout[1]) > 0)
        return  $airbnb_total_payout[1] ;

    preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night); //price per night 
     if (strlen( $airbnb_per_night[1]) > 0)
        return  $airbnb_per_night[1] ;

    preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total); //booking price
     if (strlen($booking_total[1]) > 0)
        return $booking_total [1];
    return false;
}
要调用函数,请使用:

$myValueResult = getValue($comments,$airbnb_total_payout,$airbnb_per_night,$booking_total);

$myValueResult将具有函数的结果值。

您已经获得了代码。你可以把它放到一个函数中

function getValue ($comments) {
    preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout); 
     if (strlen( $airbnb_total_payout[1]) > 0)
        return  $airbnb_total_payout[1] ;

    preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night); //price per night 
     if (strlen( $airbnb_per_night[1]) > 0)
        return  $airbnb_per_night[1] ;

    preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total); //booking price
     if (strlen($booking_total[1]) > 0)
        return $booking_total [1];
    return false;
}

(从电话中,注意错误)

您已经获得了代码。你可以把它放到一个函数中

function getValue ($comments) {
    preg_match("/Total Payout:(.*)/", $comments, $airbnb_total_payout); 
     if (strlen( $airbnb_total_payout[1]) > 0)
        return  $airbnb_total_payout[1] ;

    preg_match("/Payout:(\s+[^\s]+)/", $comments, $airbnb_per_night); //price per night 
     if (strlen( $airbnb_per_night[1]) > 0)
        return  $airbnb_per_night[1] ;

    preg_match("/Total reservation amount:(\s+[^\s]+)/", $comments, $booking_total); //booking price
     if (strlen($booking_total[1]) > 0)
        return $booking_total [1];
    return false;
}
(从电话中,注意错误)

尝试以下方法:

    if($airbnb_total_payout[1] != ""){
        echo $airbnb_total_payout[1];
    }elseif($airbnb_per_night[1] != ""){
        echo $airbnb_per_night[1] ;
    }elseif($booking_total[1] != ""){
        echo $booking_total;
    }else{
        echo "Nothing has a value";
    }
试试这个:

    if($airbnb_total_payout[1] != ""){
        echo $airbnb_total_payout[1];
    }elseif($airbnb_per_night[1] != ""){
        echo $airbnb_per_night[1] ;
    }elseif($booking_total[1] != ""){
        echo $booking_total;
    }else{
        echo "Nothing has a value";
    }

那么哪个变量将用该值保存完成的结果?@IlanHasanov您可以调用如下函数:$myValue=getValue($comments,$airbnb\u total\u payout,$airbnb\u per\u night,$booking\u total);然后$myValue将包含结果。我需要类似全局函数的东西,这样我就可以一直使用它time@IlanHasanov什么意思?你可以一次又一次地重复使用这个函数,根据你给它的参数,它会返回相应的结果。哪个变量会用这个值保存完成的结果?@IlanHasanov你可以这样调用这个函数:$myValue=getValue($comments,$airbnb\u total\u payout,$airbnb\u per\u night,$booking\u total);然后$myValue将包含结果。我需要类似全局函数的东西,这样我就可以一直使用它time@IlanHasanov什么意思?您可以一次又一次地重用该函数,并根据您给它的参数,它将返回适当的结果。