Home >Backend Development >PHP Problem >How to convert PHP collection to array

How to convert PHP collection to array

PHPz
PHPzOriginal
2023-04-13 09:20:13496browse

In PHP, a collection is a data type used to store a set of unique elements. Although there are some similarities to arrays, sets are characterized by unique elements and do not preserve the order of the elements. In some cases, it is necessary to convert the collection into an array to facilitate subsequent operations. This article will introduce how to convert a PHP collection into an array.

  1. Use the toArray() method of the collection object

PHP provides a simple method to convert the collection into an array using the toArray() method of the collection object . This method returns an array containing all elements in the collection.

The following is the sample code:

$set = new Set(['apple', 'banana', 'grape']);
$array = $set->toArray();
print_r($array);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
)
  1. Use the getValues() method of the collection object

Except In addition to using the toArray() method above, you can also use the getValues() method of the collection object to get all the values ​​in the collection and store them in an array. Unlike the toArray() method, the keys in the array returned by the getValues() method are based on the position of each value in the original collection.

The following is the sample code:

$set = new Set(['apple', 'banana', 'grape']);
$array = $set->getValues();
print_r($array);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
)
  1. Using iterators

In PHP, collections follow Standard iterator interface. Therefore, you can use the foreach loop in PHP to convert a collection into an array. The principle of this method is to iterate through the elements in the collection and add each element to a new array.

The following is the sample code:

$set = new Set(['apple', 'banana', 'grape']);
$array = [];
foreach ($set as $value) {
    $array[] = $value;
}
print_r($array);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
)

Summary:

The above three methods can convert the collection in PHP into array. Among them, using the toArray() and getValues() methods of the collection object is the most convenient, while using iterators to traverse the collection requires writing more code. When writing code, you can choose the corresponding method to convert the collection according to actual needs.

The above is the detailed content of How to convert PHP collection to array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn