Working with lists without using Array() in PHP

Asked

Viewed 1,114 times

16

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

Is there any other way to work with PHP object lists, like the Collections classes (List, Map, etc.)?

3 answers

16

Starting with php 7.2, a new extension to work with more rigid data structures. Existing types are:

  • Hashable
  • Sequence
  • Vector
  • Deck
  • Map
  • Pair
  • Set
  • Stack
  • Queue
  • Priorityqueue

Yes there are other data structures beyond the standard Array(), they’re in the library SPL, which comes by default from PHP5.0.

Some types are:

  • Splstack
  • Splqueue
  • Splheap
  • Splmaxheap
  • Splminheap
  • Splpriorityqueue
  • Splfixedarray
  • Splobjectstorage
  • Ah, boy! I barely saw your moves :D

14


Yes, there are some standard classes (from the SPL-Standard PHP Library) in PHP that do this job very well.

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

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

Here are a few 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 a common array, it adds at the beginning.

Example:

$stack = new SplStack;

$stack[] = 1;

$stack[] = 2;

$stack[] = 3;

print_r(iterator_to_array($stack));

That would return:

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

Splfixedarray

The class SplFixedArray is used to determine a fixed number of elements for a array. You determine in the instance what is the number of elements that will be present in this structure. It is similar to Array javascript, since you cannot name the indexes, but the indexes are only numerical. If you try to put a larger size than expected, an exception will be thrown.

Example:

$arr = new SplFixedArray(3);

$arr[0] = 0;

$arr[1] = 1;

$arr[2] = 2;

Some people say they’ve done tests and achieved a savings 33% less than a array but that’s the kind of stuff I don’t 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 an 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 cited by @rray.

  • I wonder why there is an example of Splstack .... D

  • I think I get your joke @rray :D

  • The responses of rray and bigown are excellent, but you exemplified the use of some of the possibilities.

7

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

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

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

Some people have done this before, since they also considered the SPL to be crap.

  • I wish I knew what was wrong.

  • Didn’t get the -1 either.

  • 2

    It must be because I said the library looks bad. One must think it’s great. That’s why "everyone is using in their codes".

  • 4

    It is so good that in most pages there is explanation of functions and features that you can not use because of bugs :)

  • 2

    But apparently the people liked her anyway :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.