douzi2785 2013-08-20 14:33
浏览 49
已采纳

php数组中的多个维度

I wanna know if it's possible to have multiple dimensions in a php array. I have 7 columns in my database and I need the data to a respective variable code:

$months = Array();
    $months = ['January'=>array(), 'February'=>array(), 'March'=>array(), 'April'=>array(), 'May'=>array(), 'June'=>array(), 'July'=>array(), 'August'=>array(), 'September'=>array(), 
                        'October'=>array(), 'November'=>array(), 'December'=>array() ]; 



    // Connect to MySQL
         if ( !( $database = mysql_connect( "localhost",
            "root", "" ) ) )                      
            die( "Could not connect to database </body></html>" );

    // open Events database
         if ( !mysql_select_db( "Events", $database ) )
            die( "Could not open Events database </body></html>" );

            foreach($months as $month => $arr) {



            $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$month'  ") 
                    or die ('Error updating database because: '.mysql_error());
            }
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $months[$month][] = $row['DayNum'];


    }
}   

So you can see $months is a two dimensional array containing a month and the days for that month in my database. But in my database I have other stuff like EVENTNAME, STARTTIME, ENDTIME, DESCRIPTION ETC...and I want to store those in a way like:

May 13th 12pm have the following events => all the events. Help please, thanks.

  • 写回答

1条回答 默认 最新

  • douke1954 2013-08-20 14:48
    关注

    First of all, there is same mess around your for cycle: $i is unused and while is outside the cycle. If you need something like Month - Day - Hour - Event, you need a four dimension array, like:

        foreach($months as $month => $arr) {
            for($i = 1; $i <=31; $i++){ // you can still work around this "for" cycle: it isn't the best option
            $months[$month][$i] = array(); // third dimension
                $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$month' AND DayNum='$i' ") 
                         or die ('Error updating database because: '.mysql_error());
                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                        $months[$month][$i][strftime('%H',$row['STARTTIME'])] = $row;    // fourth dimension
            }
            mysql_free_result($result);
            }
    }
    

    You'll get ENDTIME and DESCRIPTION as the fourth dimension of the array. You can find their values like $months['January'][13]['12']['DESCRIPTION'] or $months['January'][13]['12']['ENDTIME'].

    About the strftime('%H',$row['STARTTIME']): this is a function that gets only the hour from $row['STARTTIME'], assuming this is stored as a TIMESTAMP data type. You can substitute it with substr($row['STARTTIME'],0,2) if data type is TIME.

    I also suggest you to use mysqli or PDO instead of mysql client, as it is deprecated in PHP 5.5 and newer versions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效