php
  • arrays
  • 2015-09-01 50 views -5 likes 
    -5

    我在PHP新手...有4個數組,我想顯示,但它不顯示任何內容....foreach中的多個賦值數組?

    請與代碼

    $new_strip_from_name[] = $strip_from_name; 
    $new_from_name[] = $from_name; 
    $new_to_name[] = $to_name; 
    $new_lastdata[] = $lastdata; 
    
    echo "<table style='border: 2px solid black; text-align:left'>";  
        foreach($new_from_name as $from_name2){ 
         foreach($new_lastdata as $lastdata2){ 
          foreach($new_to_name as $to_name2){ 
           foreach($new_strip_from_name as $key => $value){     
            if((strpos($lastdata2, $value) !==FALSE)){     
            echo "<tr>"; 
            echo "<td>$from_name2</td>"; 
            echo "<td>$to_name2</td>"; 
            echo "<td>$lastdata2</td>"; 
            echo "</tr>"; 
            } 
    
           } 
          } 
         } 
        } 
    echo "</table>"; 
    

    樣品陣列指

    $new_strip_from_name = array("okay"); 
    $from_name; = array("john", "mar", "jeff"); 
    $to_name; = array("phil", "india", "japan"); 
    $lastdata; = array("[email protected]", "[email protected]", "[email protected]"); 
    

    期望的輸出

    ​​
    +4

    什麼是你想要的輸出,請舉例。輸入數組也是例子。因爲我不認爲像這樣嵌套foreach循環會生成你想要的東西。 – RiggsFolly

    +0

    顯示您的HTML文本輸入代碼。 –

    +0

    做foreach {} foreach而不是foreach {foreach {}}。這在這裏沒有意義! –

    回答

    0

    好吧,現在我們看到你想要的東西!但它不會像這樣工作。最好的辦法是創建一個類 - >http://php.net/manual/en/language.oop5.basic.php。但這裏一個基本的解決方案,它可以工作:

    $messages = array(); 
    $message = array(); 
    $message['from'] = "john"; 
    $message['to'] = "phil"; 
    $message['lastdata'] = "[email protected]"; 
    
    array_push($messages, $message); 
    

    這樣做對每封郵件或不管它是什麼,然後這樣的:

    foreach ($messages as $message){ 
        if (strpos($message,"okay") !== false){ 
         echo "<tr>"; 
         echo "<td>".$message['from']."</td>"; 
         echo "<td>".$message['to']."</td>"; 
         echo "<td>".$message['lastdata']."</td>"; 
         echo "</tr>"; 
        } 
    } 
    
    +0

    所以它的東西是這樣的? $ messages = $ from_name。 $ to_name。 $ last_data;請問您可以參考代碼 – Bee

    0

    這anwser應該與當前數據結構的工作,但這不是清潔。這不是應該的方式,每個消息應該有1個元素,而不是4個消息的4個數組,這可能會導致一些嚴重的混淆問題。如果可能,您應該更改數據的收集或​​創建。但無論如何嘗試這個:

    $new_strip_from_name[] = $strip_from_name; 
    $new_from_name[] = $from_name; 
    $new_to_name[] = $to_name; 
    $new_lastdata[] = $lastdata; 
    
    echo "<table style='border: 2px solid black; text-align:left'>"; 
    for ($i = 0; $i < sizeof($new_from_name); $i++){ 
        if (strpos($new_lastdata[$i],"okay") !== false){ 
        echo "<tr>"; 
        echo "<td>".$new_from_name[$i]."</td>"; 
        echo "<td>".$new_to_name[$i]."</td>"; 
        echo "<td>".$new_lastdata[$i]."</td>"; 
        echo "</tr>"; 
        } 
    } 
    echo "</table>"; 
    
    +0

    先生,請將您的答案插入參考代碼中,.... – Bee

    相關問題