- Chapter 3: Using Arrays
- 1 Declaring an Array
- 2 Printing Out an Array
- 3 Eliminating Duplicate Elements
- 4 Enlarging or Shrinking an Array
- 5 Merging Arrays
- 6 Iteratively Processing Elements in an Array
- 7 Accessing the Current Element in an Array
- 8 Accessing Different Areas of an Array
- 9 Searching an Array
- 10 Searching for the Different Elements in Two Arrays
- 11 Randomizing the Elements of an Array
- 12 Determining the Union, Intersection, or Difference Between Two Arrays
- 13 Sorting an Array
- 14 Sorting Sensibly
- 15 Reversing Order
- 16 Perl-Based Array Manipulation Features
3.16 Perl-Based Array Manipulation Features
You're a "Perl monger" and you want to know what array features PHP has that are related to Perl.
Technique
Here is a list of all the Perl-related array manipulation features, as well as (in the "Discussion" section) a list of incompatibilities with their Perl counterparts.
Perl Function PHP Function(s)
pop() array_pop()
push() array_push()
shift() array_shift()
unshift() array_unshift()
splice() array_splice()
join() join(), implode()
grep() preg_grep()
keys() array_keys()
each() each()
values() array_values()
reverse() array_reverse()
sort() usort() (to some extent)
Comments
There are some incompatibilities between certain functions; for example, between Perl's push() function and PHP's array_push() function. This is more of a general incompatibility. In Perl, arrays specified in list context are automatically flattened, so
push (@list1, @list2);
would combine the two arraysthe same thing that array_merge() would do in PHP. However, with array_push() this example would create a two-dimensional array because PHP does not automatically flatten its arrays.