Working with lists without using Array () in PHP

16

I know that in PHP for almost every type of list, we work with Array() , but is this the only way to work with lists in PHP?

Is there any other way to work with lists of objects in PHP, similar to the Collections classes ( List , Map , etc)?

    
asked by anonymous 26.01.2016 / 14:33

3 answers

12

Yes, there are some standard PHP (Standard PHP Library) classes in PHP that do the job well.

Generally these classes implement the interfaces ArrayAccess , Iterator or IteratorAggregate .

The ArrayAccess interface allows access and definition of class members as if it were a array .

Here are some examples:

SplStack

This class works like a stack. When you add an element via SplStack $stack[] , instead of adding the element at the end as it does in an ordinary array, it adds to the beginning.

Example:

$stack = new SplStack;

$stack[] = 1;

$stack[] = 2;

$stack[] = 3;

print_r(iterator_to_array($stack));

This would return:

[
     2 => 3,
     1 => 2,
     0 => 1,
 ]

SplFixedArray

The SplFixedArray class is used to determine a fixed number of elements for a array . You determine in the instance how many elements will be present in that structure. It is similar to the Array of javascript, since you can not name the indexes, but the indexes are just numeric. If you try to place a larger-than-expected size, an exception will be thrown.

Example:

$arr = new SplFixedArray(3);

$arr[0] = 0;

$arr[1] = 1;

$arr[2] = 2;

Some people say that they did tests and got an economy 33% less than a% of common%, but that's the kind of thing I do not usually focus on.

SplObjectStorage

This class is for you to store objects. The big difference is that when you define an object in it, it is passed as the index of that "structure".

 $s = new SplObjectStorage();

$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;

$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);

These are some examples, but there are others, which have already been mentioned by @rray.

    
26.01.2016 / 14:45
16

As of php 7.2, a new extension is available to work with data structure more rigid. The existing types are:

  • Hashable
  • Sequence
  • Vector
  • Deque
  • Map
  • Pair
  • Set
  • Stack
  • Queue
  • PriorityQueue

If there are other data structures besides the Array() standard, they are in the SPL library , which comes as standard since PHP5.0.

Some types are:

  • SplStack
  • SplQueue
  • SplHeap
  • SplMaxHeap
  • SplMinHeap
  • SplPriorityQueue
  • SplFixedArray
  • SplObjectStorage
26.01.2016 / 14:37
8

By default it does not exist. Nothing prevents you from creating new types that specifically implement each of these related structures and algorithms. Probably using the array specifically within the structure.

You can also use some library that already implements this for you, such as the one mentioned by rray (which seems to be pretty bad, by the way: P) which is an extension.

In theory it is also possible to implement some structure in C and expose it to PHP, but almost nobody does this. It would have a very interesting performance advantage.

Some people have already done this . Since they also considered SPL to be crap.

    
26.01.2016 / 14:39