Home Backend Development PHP Problem How to convert php files to array

How to convert php files to array

Apr 25, 2023 am 09:05 AM

When writing PHP programs, we often need to read files and convert them into arrays to facilitate data manipulation. Converting php files to arrays is a common task and can be accomplished through the following methods.

1. Use the file_get_contents function
The file_get_contents function can read the file as a string. We can read the file into a string and then use the explode function to split the string into an array based on the delimiter we require. For example, we have a file data.txt which contains the following content:

Tom:24
Jerry:25
Kate:28
Copy after login
Copy after login
Copy after login

You can use the following code to convert it to an array:

$data = file_get_contents('data.txt');
$data_array = explode("\n", $data);
foreach ($data_array as &$value) {
    $value = explode(':', trim($value));
}
unset($value);
print_r($data_array);
Copy after login

The output is as follows:

Array
(
    [0] => Array
        (
            [0] => Tom
            [1] => 24
        )

    [1] => Array
        (
            [0] => Jerry
            [1] => 25
        )

    [2] => Array
        (
            [0] => Kate
            [1] => 28
        )

)
Copy after login
Copy after login
Copy after login

2. Use the file function
The file function reads the entire file as an array, and each array element is a line in the file. Therefore, we can directly use the file function to read the file as an array. For example, we have a file data.txt which contains the following content:

Tom:24
Jerry:25
Kate:28
Copy after login
Copy after login
Copy after login

You can use the following code to convert it to an array:

$data_array = file('data.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($data_array as &$value) {
    $value = explode(':', trim($value));
}
unset($value);
print_r($data_array);
Copy after login

The output is as follows:

Array
(
    [0] => Array
        (
            [0] => Tom
            [1] => 24
        )

    [1] => Array
        (
            [0] => Jerry
            [1] => 25
        )

    [2] => Array
        (
            [0] => Kate
            [1] => 28
        )

)
Copy after login
Copy after login
Copy after login

3. Use the fgets function
The fgets function can read the file content line by line. We can use the fgets function in a loop to read the file as an array. For example, we have a file data.txt which contains the following content:

Tom:24
Jerry:25
Kate:28
Copy after login
Copy after login
Copy after login

You can use the following code to convert it to an array:

$handle = fopen('data.txt', 'r');
$data_array = array();
while (!feof($handle)) {
    $line = fgets($handle);
    if ($line !== false) {
        $line = explode(':', trim($line));
        $data_array[] = $line;
    }
}
fclose($handle);
print_r($data_array);
Copy after login

The output is as follows:

Array
(
    [0] => Array
        (
            [0] => Tom
            [1] => 24
        )

    [1] => Array
        (
            [0] => Jerry
            [1] => 25
        )

    [2] => Array
        (
            [0] => Kate
            [1] => 28
        )

)
Copy after login
Copy after login
Copy after login

The above three methods can all convert files into arrays. Which method to choose depends on the actual application scenario. If the file content is small, it is recommended to use the file_get_contents or file function to read the file into a string or array, and then convert it to an array operation; if the file content is large, it is recommended to use the fgets function to read the file content line by line.

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles