【问题标题】:Having two arrays in variable php在变量php中有两个数组
【发布时间】:2015-06-22 13:14:09
【问题描述】:

我有这个代码,我正试图爆炸我的约会 例如“2015 年 6 月 4 日” 在其他 2 个变量中有日期和月份,但我有一个方法,有一个数组,是否有可能, 我的代码不起作用,它写“Array[0]”为什么?

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded[$i] = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

编辑

在这种情况下:

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

       echo" 
                              <div class=\"circleBase type3\"><div style=\"text-align:center;\">
                              <br>
                             <font size=\"+2\" style=\"margin-left:4px;\"> $data_exploded[0] </font>

它正确地返回“04”,但我不能使用它,因为它为我的循环的每个结果重复 04。

所有代码

  <div id="container">



<?php 
//VARIABILI DI LOGIN CON FACEBOOK 

$FBID = $_GET['FBID'];
$FBUSER = $_GET['FBUSER'];
$FEMAIL = $_GET['FEMAIL'];
$DBhost = "localhost";
$DBuser = "bestparty";
$DBpass = "";
$DBName = "my_bestparty";

$count = strlen($FBID);

if ($count < 5 || !$count || $count = "undefined") {}
else {


//CHECK IF USERS EXIST
$link = mysql_connect("localhost", "bestparty", "") or die(mysql_error()); 
                                mysql_select_db("my_bestparty") or die(mysql_error()); 


                        $myquery = "SELECT * FROM Utenti WHERE Fuid = '$FBID'";

                        $resulta = mysql_query($myquery);
                        $numbera = mysql_num_rows($resulta);
                        mysql_close($link);
                        if($numbera == '0'){
                        $link1 = mysql_connect("localhost", "bestparty", "password") or die(mysql_error()); 
                                mysql_select_db("my_bestparty") or die(mysql_error()); 


                        $myquery1 = "INSERT INTO `Utenti`(`UID`, `Fuid`, `Ffname`, `Femail`, `class`) VALUES (NULL,'$FBID','$FBUSER','$FEMAIL','3')";

                        $resulta1 = mysql_query($myquery1);
                        $numbera1 = mysql_num_rows($resulta1);
                        mysql_close($link1);

                        }
                        else {

                        }
}







$table = "Database";

mysql_connect($DBhost,$DBuser,$DBpass) or die("mysql_error()");
@mysql_select_db("$DBName") or die("mysql_error()");



$sqlquery = "SELECT * FROM `Database` ORDER BY data DESC";
$result = mysql_query($sqlquery);
$number = mysql_num_rows($result);



$i = 0;

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

       echo" <style>
           .circleBase {
        border-radius: 50%;
        behavior: url(PIE.htc); /* remove if you don't care about IE8 */
        }

        .type1 {
        width: 100px;
        height: 100px;
        background: yellow;
        border: 3px solid red;
        }
        .type2 {
        width: 50px;
        height: 50px;
        background: #ccc;
        border: 3px solid #000;
        }
        .type3 {
        position: absolute;
        width: 60px;
        height: 60px;
        margin-left:40%;
        margin-top: 53%;
        background: #fff;
        z-index: 1000;
        box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);
        font-size:8px;
        color: #e20a7e;
        border: solid 1px #e20a7e;
        }
            </style>
                                 <!--INIZIO DIV EVENTO-->
                              <div class=\"circleBase type3\"><div style=\"text-align:center;\">
                              <br>
                             <font size=\"+2\" style=\"margin-left:4px;\"> $data_exploded[0] </font>
                              <br>Maggio</div></div>
                                <a href=\"pagevento.html?ID=$idfesta[$i]&FBID=$FBID&FBUSER=$FBUSER\">
                                    <div style = \"display: block; margin: 0 auto; width: 95%; \">
                                        <div id=\"testihomeevento\"
                                         style=\" box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);
                                          padding:10px; display: block; margin: 0 auto; width: 85%; background-color: #fff;font-size: 0.7em;\">
                                            <font size=\"+1\" style=\"color:#e20a7e\">$festa[$i]</font> <br>
                                            $luogo[$i] <br>
                                            $data[$i] <br>
                                        </div>

                                        <div style=\"background-image:url(http://www.bestparty.altervista.org/APP/foto/$nomeimg[$i]);
                                                         background-position:center;
                                                       background-size: cover;
                                                       position: relative;
                                                       width: 100%;
                                                       height: 240px;
                                                       box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);\">

                                        </div>


                                        <div style=\" display: block; margin: 0 auto; width: 85%; background-color: #fff;
                                                    box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);\">
                                            <br>
                                            <br>
                                        </div>  

                                    </div>
                                    <br>

                                </a>

                                 <!--FINE DIV EVENTO--> 

";

$i++;
}
?>                      
                                </div>

【问题讨论】:

  • $data[$i] = data_eng_to_it_($data[$i]); 在做什么?
  • 为了清楚起见,请重新格式化您的问题。
  • 你在使用什么命令it writes "Array[0]"?在 PHP 中打印或显示数据有很多不同的方法,我想知道您的输出是否只是由使用行为与您预期不同的输出函数引起的。
  • 如果我执行 "echo $data_exploded[$i][0]" 而不是打印 "04" 它会打印 "Array[0]" @Tor
  • $data[$i] = data_eng_to_it_($data[$i]);从 2015 年 6 月 4 日转换为 2015 年 6 月 4 日 @JonathanClark

标签: php mysql arrays variables multidimensional-array


【解决方案1】:

explode 功能使用正确,所以你的问题更进一步。

$data[$i] = mysql_result($result,$i,"data"); 没有从数据库返回预期的字符串“2015-06-04”

或

您的函数 $data[$i] = data_eng_to_it_($data[$i]); 未返回预期的字符串“2015 年 6 月 4 日”

因此,在这两行之后通过 echo / var_dump 进一步测试,以确保您获得了您期望的字符串。 在data_eng_to_it_ 函数中你还记得return $data 吗?


编辑:

您正在回显$data_exploded[0],它将输出“数组”。而是输出二维数组,因此 $data_exploded[0][0] 将输出“04”。或者要查看 $data_exploded[0] 的完整输出,请使用:var_dump($data_exploded[0]);

要让它在您的 while 循环中工作,请使用 $data_exploded[$i][0]

但是,您不能在回显字符串“”中执行此操作。使用. 连接它或将其分配给更简单的变量。

  1. 更简单的变量:$day = $data_exploded[$i][0] 并以与您相同的方式使用 day 变量
  2. 连接:echo "&lt;h1&gt;".$data_exploded[$i][0]."&lt;/h1&gt;";
  3. 拆分 HTML 和 PHP:我个人会这样做:?&gt; &lt;h1&gt;&lt;?php echo $data_exploded[$i][0];?&gt;&lt;/h1&gt;

【讨论】:

  • 当我在 "$data[$i] = data_eng_to_it_($data[$i]);" 之后回显 $data[$i]我得到正确的“2015 年 6 月 4 日”,当我回显 $data_exploded[0] 它返回我“数组”
  • 也许您应该发布更多代码。如果此时您得到“2015 年 6 月 4 日”,则在explode 函数后echo $data_exploded[$i][0]; 应返回“04”
  • 如果我回显 $data_exploded[0][0] 或 $data_exploded[0][$i] 作为输出“04[0]”
  • 和 $data_exploded[$i][0] ?
  • 它也返回“04[0]”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多