【问题标题】:how to print out two arrays in php concatenating them如何在php中打印出两个数组连接它们
【发布时间】:2012-08-08 22:41:42
【问题描述】:

我的 php 中有两个要打印的数组。我希望将它们连接起来,但我不知道如何连接。 $names 的数组打印,但描述数组“$desc”不打印。有什么办法可以同时打印吗?

    $query = "SELECT eName FROM Events";
            $query2 = "SELECT eDescription FROM Events";
            $result = mysql_query($query);
            $result2 = mysql_query($query2);
            $names = array();
            $desc = array();
            echo "hello there people!" . $query . " ".$result;
            for($i=0; $i<sizeof($result); $i++){
                echo $result[$i] ."\n" . $result2[$i];
            }
            while($entry = mysql_fetch_row($result)){
                $names[] = $entry[0];
            }
            while($entry2 = mysql_fetch_row($result2)){
                $desc[] = $entry2[0];
            }
            echo "Which Event would you like to see?<br>";
            $stop = count($names);
            //echo $stop . "\n";
            $i = 0;
            print_r($names);                
            print_r($desc);
            foreach($names as $value){
                    echo $value . " " . $desc[i] ."<br>";   
                                     $i++;              
            }

【问题讨论】:

  • 你的循环索引应该是$i,而不是i

标签: php arrays printing echo


【解决方案1】:

为什么要执行两个查询以从同一来源获取数据?

$sql = mysql_query("select `eName`, `eDescription` from `Events`");
while($row = mysql_fetch_assoc($sql)) {
    echo $row['eName']." ".$row['eDescription']."<br />";
}

简单得多。

【讨论】:

【解决方案2】:

试试这个:

        foreach($names as $key => $value){
                echo $value . " " . $desc[$key] ."<br />";                 
        }

只要数组$key匹配,信息就会一起打印出来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多