2

I need to return a single result that combines the statements created from multiple arrays through multiple WHILE loops.

I have four arrays and am trying to get different outputs based on a condition. The arrays are all structured the same keys as one another, and the keys correspond to one event. One of the arrays may have multiple values per key, and the number of values inside this particular array needs to be checked to use a different output structure if it contains more than one value.

foreach ($closures as $closure) {

//stuff

    for ( $i = 0; $i < $count; $i++){        
        $vcount = count( $locations[$i] );

        while ( $vcount < 2 ) {
            $result = "The ".$venue[$i]." has been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";
            //print_r($result); //looks good printed...  
            break;
        }

        while ($vcount > 1 ) {
            $result =  "The ".$venue[$i]."s have been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";
            //print_r($result); //looks good printed... 
            break;
        }
    }
    return $result;
    break;
}

I've tried every combination of "break," and of course it gives me different outputs but none are correct. I've tried assigning different variables to each while loop ($result1 and $result2). I've tried replacing $result with $result[$i] inside and outside the loops. I've tried moving the placement of "return...".

While using print_r as shown it displays perfectly, but I cannot figure out how to put those results into a single output.

I've only been able to get the first result to output through my shortcode, and can only manage to get two results (one from each loops) when trying to combine them into a statement like:

$message = "".result1."".result2."";
4

1 回答 1

2

您可以使用 .= 赋值运算符将结果值连接到彼此

foreach ($closures as $closure) {

//stuff

for ( $i = 0; $i < $count; $i++){        
    $vcount = count( $locations[$i] );
    // initiate the result
    $result = "";
    while ( $vcount < 2 ) {
        // concatinate the value to $result
        $result .= "The ".$venue[$i]." has been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";
        //print_r($result); //looks good printed...  
        break;
    }

    while ($vcount > 1 ) {
        // concatinate the value to $result again
        $result .=  "The ".$venue[$i]."s have been reserved for an <a href=".$eventURLs[$i].">event</a> today from ".$startTime[$i]." - ".$endTime[$i].".<br>";
        //print_r($result); //looks good printed... 
        break;
    }
}
return $result;
break;
}
于 2019-08-23T10:32:36.790 回答