How to convert PHP collection to array

PHPz
Release: 2023-04-13 10:20:34
Original
428 people have browsed it

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);
Copy after login

The output result is:

Array ( [0] => apple [1] => banana [2] => grape )
Copy after login
Copy after login
Copy after login
  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);
Copy after login

The output result is:

Array ( [0] => apple [1] => banana [2] => grape )
Copy after login
Copy after login
Copy after login
  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);
Copy after login

The output result is:

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

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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!