spl

Iterate in reverse through an array with PHP - SPL solution?

对着背影说爱祢 提交于 2019-11-27 20:35:20
Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it? I could simply do $array = array_reverse($array); foreach($array as $currentElement) {} or for($i = count($array) - 1; $i >= 0; $i--) { } But is there a more elegant way? There is no ReverseArrayIterator to do that. You can do $reverted = new ArrayIterator(array_reverse($data)); or make that into your own custom iterator, e.g. class ReverseArrayIterator extends ArrayIterator { public function __construct(array $array) { parent::__construct(array_reverse($array)); } } linepogl Here is a solution

OutOfRangeException vs. OutOfBoundsException

允我心安 提交于 2019-11-27 20:22:54
问题 PHP defines two SPL exceptions for invalid keys: OutOfRangeException: Exception thrown when an illegal index was requested. This represents errors that should be detected at compile time. OutOfBoundsException: Exception thrown if a value is not a valid key. This represents errors that cannot be detected at compile time. As PHP isn't a compiled language the distinction between compile-time and run-time seems strange and thus I am finding it hard to understand which exception to use when.

PHP关于SPL学习经历

懵懂的女人 提交于 2019-11-27 18:24:54
<?php namespace App\Http\Controllers; class WelcomeController extends Controller { use \App\Traiter\TraitTest;// 使用traitpublic function show() { $iterator = new \NoRewindIterator($this->yieldTest());//生成一个Iterator foreach ($iterator as $key => $value) { var_dump($value); } $this->myTest(); $fileIterator = new \NoRewindIterator($this->splFileObject()); foreach ($fileIterator as $key => $value) { var_dump($value); } $path = "../"; $this->splFileObjec($path);// $this->recDirectoryIterator($path); $recArrayIterator = $this->recArrIterator(); $this->recIterIterator($recArrayIterator); $this-

after using $files = new DirectoryIterator() in PHP, how do you sort the items?

。_饼干妹妹 提交于 2019-11-27 08:03:29
问题 We can get the files in a directory in PHP by $files = new DirectoryIterator() after that is there an easy way to sort the items in a particular order for displaying them? thanks. 回答1: It doesn't look like there is a way to sort the data within the iterator. You could place the display data into an intermediary array, with a key of the value you wish to sort by, and call ksort() on the array. This will take two passes over the data however. $path = "."; $files = new DirectoryIterator($path);

RuntimeException SplFileInfo::getSize(): stat failed for… Laravel 4 upload image

[亡魂溺海] 提交于 2019-11-27 07:55:32
问题 I work with laravel 4 (last update) , I create a form where we could upload an image (logo/avatar). I am on MAC OS, I use sublime Text 3 , laravel and MAMP Applications . All my configuration is setting right, no running problems. My probleme is that I have this error when I submit the reaching fields from my form: RuntimeException SplFileInfo::getSize(): stat failed for /Applications/MAMP/tmp/php/phpRUJfMX Here's the code from my form nameOfTheForm.blade.php : @extends('layout.default')

“Indirect modification of overloaded element of SplFixedArray has no effect”

倖福魔咒の 提交于 2019-11-27 02:32:03
问题 Why the following $a = new SplFixedArray(5); $a[0] = array(1, 2, 3); $a[0][0] = 12345; // here var_dump($a); produces Notice: Indirect modification of overloaded element of SplFixedArray has no effect in <file> on line <indicated> Is it a bug? How do you deal with multidimensional SplFixedArrays then? Any workarounds? 回答1: First, the problem is related to all classes which implement ArrayAccess it is not a special problem of SplFixedArray only. When you accessing elements from SplFixedArray

Peek ahead when iterating an array in PHP

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:22:40
问题 Is it possible to "peek ahead" while iterating an array in PHP 5.2? For example, I often use foreach to manipulate data from an array: foreach($array as $object) { // do something } But I often need to peek at the next element while going through the array. I know I could use a for loop and reference the next item by it's index ( $array[$i+1] ), but it wouldn't work for associative arrays. Is there any elegant solution for my problem, perhaps involving SPL? 回答1: You can use the

Iterate in reverse through an array with PHP - SPL solution?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:56:55
问题 Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it? I could simply do $array = array_reverse($array); foreach($array as $currentElement) {} or for($i = count($array) - 1; $i >= 0; $i--) { } But is there a more elegant way? 回答1: There is no ReverseArrayIterator to do that. You can do $reverted = new ArrayIterator(array_reverse($data)); or make that into your own custom iterator, e.g. class ReverseArrayIterator extends ArrayIterator { public

Sort directory listing using RecursiveDirectoryIterator

蓝咒 提交于 2019-11-26 21:47:30
问题 I'm using RecursiveDirectoryIterator and RecursiveIteratorIterator to build a file listing tree using code like below. I need to the list to be sorted - either directories then files alphabetically, or just alphabetically. Can anyone tell me how to sort the file list? $dir_iterator = new RecursiveDirectoryIterator($groupDirectory); $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $file) { // do stuff with $file } 回答1: This

How can I retrieve the full directory tree using SPL?

邮差的信 提交于 2019-11-26 20:44:09
How can I retrieve the full directory tree using SPL , possibly using RecursiveDirectoryIterator and RecursiveIteratorIterator ? By default, the RecursiveIteratorIterator will use LEAVES_ONLY for the second argument to __construct . This means it will return files only. If you want to include files and directories (at least that's what I'd consider a full directory tree), you'd have to do: $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST ); and then you can foreach over it. If you want to return the directory tree instead