一旦满足某个条件,有没有办法阻止内爆?我在任何文档中都找不到任何内容。此代码生成的输出如下所示:
page ---> page ---> $goal ---> page ---> $goal
page ---> $goal ---> page ---> page
我希望它只在第一个$目标出现之前运行,然后移到下一行,所以这是最后一个这样的:
page ---> page ---> $goal
page ---> $goal
这是我的代码部分与内爆。我厌倦了在回声之前坚持使用if语句,但是没有做任何事情。
$goalpage = "leadfactory.ca/analytics/goal.php";
$html = "<table>";
$html .= "<tr><th align='left'>PAGE PATHS HITTING GOAL.PHP</th></tr>";
$paths = array();
$sql = "SELECT cookie_uid, page_url FROM pageviews WHERE cookie_uid ORDER BY rowid";
$result = mysqli_query($conn, $sql);
$got_rows = mysqli_num_rows($result);
if ($got_rows) {
while ($row = mysqli_fetch_array($result)) {
// Create an array for the cookie_uid if it doesn't exist yet
if ( ! array_key_exists($row['cookie_uid'], $paths) || ! is_array($paths[$row['cookie_uid']])) {
$paths[$row['cookie_uid']] = [];
}
// Add to the array now that we know it exists
array_push($paths[$row['cookie_uid']], $row['page_url']);
}
foreach ($paths as $session => $page) {
if ($page == $goalpage) {
break;
}
echo "<tr><td>" . implode(' ---> ', $page) . "</td></tr>";
}
} else {
$html .= '<td colspan="2">No results</td>' . "";
}
$html .= "</table>";
echo $html;
if (!mysqli_query($conn,$sql)) {
die('Error: ' . mysqli_error($conn));
}
答案 0 :(得分:1)
// Your starting array
$pages = array("page","page","page","page","page","goal","page","page",);
// Get the index of the "goal" key
// see: http://php.net/manual/en/function.array-search.php
$lastkey = array_search("goal", $pages);
// Shorten your array accordingly
// see: http://php.net/manual/en/function.array-slice.php
$pages = array_slice($pages, 0, $lastkey+1);
// Implode
implode(' ---> ', $pages);
不,你不能停止&#34;内爆,但是在内爆之前你可以搞砸你的数组...
答案 1 :(得分:0)
你可以这样做:
$arr[0]="page0";
$arr[1]="page1";
$arr[2]="page2";
$arr[3]="goal";
$arr[4]="page4";
echo implode(" ---> ", array_slice($arr, 0, array_search("goal",$arr)+1));
输出
page0 ---> page1 ---> page2 ---> goal
看到它的实际效果 https://3v4l.org/cFhv3
该功能将是:
function implode_with_limit($arr, $goal)
{
return implode(" ---> ", array_slice($arr, 0, array_search($goal,$arr)+1));
}