2016-08-12 42 views
0

比方說,我有一個字符串我怎麼在刷新呼應一個一個數組在php中每次echo其他字符串?

session_start(); 

if(isset($_SESSION["names"])){ 
    $_SESSION["names"] = $_SESSION["names"] + 1; 
} 
else{ 
    $_SESSION["names"] = 1; 
} 

    $names = array("Adrian","Bill","Mac","John"); 

    echo $_SESSION["names"]; 

我的代碼是凌亂只是舉例。我需要呼應每次有新的名稱,但不是隨機的

回答

1

這工作對我來說:

<?php 
    session_start(); 

    if(isset($_SESSION["names"])){ 
     $_SESSION["names"] = $_SESSION["names"] + 1; 
    } 
    else{ 
     $_SESSION["names"] = 0; 
    } 

    $names = array("Adrian","Bill","Mac","John"); 

    echo $names[$_SESSION["names"] % count($names)]; 
?> 
+0

@JohnBook如果我的答案滿足您的查詢,然後將其標記爲接受? –

+0

新的stackoverflow是我標記它 –

+0

@JohnBook http://stackoverflow.com/help/someone-answers –

0

如何

session_start(); 

if(!isset($_SESSION["echoedNames"])) { 
$_SESSION["echoedNames"] = array(); 
} 

while (!in_array($randomName = $names[rand(0, count($names))], $echoedNames) { 
echo $randomName; 
$_SESSION["echoedNames"][] = $randomName; 
} 

或類似的東西..基本上兩個陣列,一個已經顯示的名稱,並檢查是否顯示之前回聲...

+0

OP說,他不想隨機的名字。 –

+0

啊,現在我明白了,我以爲他的意思不是隨機的,而是隨機的,從未顯示過..我的壞 –

相關問題