【PHP移动文件指针ftell()、fseek()、rewind()函数总结】教程文章相关的互联网学习教程文章

在php中重置for循环的指针【代码】

给出以下代码<?php $a = array(1,2,3,4,5,6); $c=0; foreach($a as $v){if($v==5&&$c==0){$c=1;reset($a);}var_dump($v); }如何重置指针以便打印1,2,3,4,5,1,2,3,4,5,6? 我知道在这种情况下我可以简单地说<?php $a = array(1,2,3,4,5,6); $c=0; for($i=0;$i<count($a);++$i){$v = $a[$i];if($v==5&&$c==0){$c=1;$i=-1; //because of the loop ++$i}var_dump($v); }但是我有一个更复杂的代码片段,解决方案并不像重写循环那样简单(...

在PHP中,在使用fopen()将文件指针资源分配给变量之后,如何从变量中获取文件名?【代码】

例如:$file = fopen("File.txt", "r"); $filename = $file->basename;如果有一个类似于文件对象的基本名称的方法(文件指针资源).解决方法:不,没有方法可做.您应该将文件名存储在变量中,如下所示:<?php $filename = "File.txt"; $file = fopen($filename, "r"); $basename = basename($filename);另外,请注意一点:文件指针不是对象,它是一种资源,您可以通过将其传递给var_dump()来看到它(它会输出类似于资源(3)的类型(流)).这意味...

如何确定变量是否包含PHP中的文件指针?【代码】

正如问题所述:如何检查PHP中的变量是否包含文件指针?有些像is_string()或is_object().解决方法:您可以使用get_resource_type() – http://us3.php.net/manual/en/function.get-resource-type.php.如果函数根本不是资源,则该函数将返回FALSE.$fp = fopen("foo", "w"); ... if(get_resource_type($fp) == 'file' || get_resource_type($fp) == 'stream') {//do what you want here }PHP文档说上面的函数调用应该返回’file’,但在...

PHP:如何在foreach循环中移动数组指针?【代码】

$animals = array('cat', 'dog', 'horse', 'elephant'); foreach($animals as $animal) {var_dump($animal);next($animals); }上面的代码输出:猫,狗,马,大象.我认为下一个函数应该移动$animals的内部指针,因此,我应该得到这个输出:cat,horse. 如何使$animals的内部指针向前(和向后)移动,以便它在foreach中受到影响? 编辑1: 从手册:As foreach relies on the internal array pointer changing it within theloop may lead to u...

PHP行为和数组指针【代码】

我正在阅读PHP手册(特别是each()功能)并遇到以下警告:CautionBecause assigning an array to another variable resets the original array’s pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop.一个例子:<?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');reset($fruit); while (list($key, $val) = each($fruit)) {echo "$key =...

如何存储和重置PHP数组指针?【代码】

我有一个关联数组,即$primes = array(2=>2,3=>3,5=>5,7=>7,11=>11,13=>13,17=>17,// ...etc );那我呢// seek to first prime greater than 10000 reset($primes); while(next($primes) < 10000) {} prev($primes);// iterate until target found while($p = next($primes)) {$res = doSomeCalculationsOn($p);if( IsPrime($res) )return $p; }问题是IsPrime还遍历$primes数组,function IsPrime($num) {global $primesto, $primes, ...

三、phper必知必会之数组指针【代码】

数组指针 1.介绍几个数组指针的函数current() - 返回数组中的当前单元 end() - 将数组的内部指针指向最后一个单元 prev() - 将数组的内部指针倒回一位 reset() - 将数组的内部指针指向第一个单元 each() - 返回数组中当前的键/值对并将数组指针向前移动一步<?php $listArr = ['1232','2456','7789','8976','5678','3456','2347','9876','3451','7744','2212','3214', ];echo "第一个元素".key($listArr).'=>'.current($listArr).P...

php – 使用元数据推送指向追随者的指针(MySQL查询)【代码】

我在StackOverflow上看过以下问题,Intelligent MySQL GROUP BY for Activity Streams由Christian Owens 12/12/12发布. 所以我决定尝试相同的方法,制作两张与他相似的表格.然后我几乎复制了我理解的查询.这是我从沙盒中得到的:Array ([0] => Array([id] => 0[user_id] => 1[action] => published_post[object_id] => 776286559146635[object_type] => post[stream_date] => 2015-11-24 12:28:09[rows_in_group] => 1[in_collection...