各位老师,初学PHP,有些问题百度找不到,便来这里请教,还望各位老师多多包涵与帮助,感激不尽。
我现在遇到的问题是这样的:
通过一个MYSQL命令,得到一个列表,并将这个列表存到一个数组。
//定义日期数组 $search_date=array(); //进入FOR循环,从开始时间循环到结束时间 //转化为UNIX时间戳进行操作 for($i=strtotime($start_time);$i<strtotime($stop_time)+86400;$i+=86400) { $search_date_num = date('Y-m-d', $i); $search_date[]= $search_date_num; }
通过上面的代码,得到一个search_date的数组。里面可能有2个以上的数据。都是世界,比如
array ([0]>='2014-08-10' [1]>='2014-08-11')
现在的问题就是,如果我通过foreach ($search_date as $v){
}
这样的逻辑去数据里面取数据。
会取到
Array ( [0] => 16777221 [1] => 16777222 ) Array ( [0] => 16777221 [1] => 16777222 )
这样的数据,不是一个完整的数组。
每次循环都是形成了一个新的数组。
有没有办法将循环得到的所有数据存成一个大的1维或者2维的数组呢,而不是每次循环都是得到一个数组呢?
回复讨论(解决方案)
$newArr= array();$search_date = array(0=>'2014-08-10' ,1=>'2014-08-11');$num = 0;foreach ($search_date as $k => $v){ $newArr[$num][$k] = $v; $num++;}var_dump($newArr);
写着玩的你可以看下思路然后自己去组装。
不太明白你的意思
请在说的清楚一点
xuzuning老师。
是有一个1维数组A,值是日期。
然后我通过foreach A这个数组。
将A的值作为条件去数据库取另一些值,这些值是一个列表,可能有几十个甚至几万个。
我需要将取到的值按顺序存为一个1维的数组。
现在遇到的问题是:
当日期是1时,取到数值列表并存进数组C;
当日期为2时,又取到一份C。
这时,打印C这个数组,数组变成了非1维数组,而是有两个数组。
如果将C变成一个1维数组,并将多次循环的结果都存进这一个数组?
你是怎么把结果保存到数组的?
$log_id = array(); foreach ($search_date as $v) { //查询当日所有符合条件的id $sql = "Select distinct log_id from $s where DATE(log_time)='".$v."'"; $result=mysql_query($sql,$link); //通过一个循环将值存入 while($arr = mysql_fetch_assoc($result)) { $log_id [] = $arr['log_id ']; }
那么 foreach 结束的时候,$log_id 不就是个一维数组吗?
是呀。可是每次循环都会生成一个1维数组。这样10次循环就有了10个1维数组。
我想要将10次循环的结果存到一个1维数组里面去。
请问有什么方法吗?
那你并没贴全这部分代码呀,我怎么知道你又做了什么?
不好意思,我贴全一点。
//定义日期数组$search_date=array();//进入FOR循环,从开始时间循环到结束时间 //转化为UNIX时间戳进行操作for($i = strtotime($start_time); $i < strtotime($stop_time)+86400;$i+= 86400){ $search_date_num = date('Y-m-d', $i); $search_date[]= $search_date_num;}//以上,会得到数组$search_date//格式为:Array ( [0] => 2014-07-21 [1] => 2014-07-22 )//然后:$log_id = array();$s =' s11';foreach ($search_date as $v) { //查询当日所有符合条件的id $sql = "Select distinct log_id from $s where DATE(log_time)='".$v."'"; $result=mysql_query($sql,$link); //通过一个循环将值存入 while($arr = mysql_fetch_assoc($result)) { $log_id [] = $arr['log_id ']; }
然后我
print_r($log_id );
得到以下数组:
Array ( [0] => 16777221 [1] => 16777222 [2] => 16777223) Array ( [0] => 26777221 [1] => 26777222 [2] => 26777223)
而我想得到这样的数组:
Array ( [0] => 16777221 [1] => 16777222 [2] => 16777223 [3] => 26777221 [4] => 26777222 [5] => 26777223)
就你贴出的这些代码,$log_id 是不可能为二维数组的
array_merge???
不是二维数组喔,是多个1维数组,每次循环都产生了一个1维数组~~
array_merge???
不是array_merge,array_merge() 函数把两个或多个数组合并为一个数组,你的不是这个效果。谢谢net8386308,我要的是每次循环产生的数组都按序存入到一个数组,在某一个循环里面是只有一个1维数组的,所以这个循环里面不可能合并。也许可以传值到一个函数里面去合并?但循环的天数不固定,那个函数也不知道该如何写。
已解决,感谢各位老师的指教。
就你贴出的这些代码,$log_id 是不可能为二维数组的
LZ再仔细分析一下你的代码吧,数组操作没那么复杂,学会调试,用最蠢的"echo"方法去调试,看下你的数据在每一次循环中都发生了什么变化。 从我这里看,你的$log_id不会是一个二维数组的。除非你的$arr['log_id ']本身就是一个一维数组,你自己echo看看吧!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft