Home  >  Article  >  Backend Development  >  How to convert php files to array

How to convert php files to array

PHPz
PHPzOriginal
2023-04-25 09:05:50500browse

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

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);

The output is as follows:

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

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

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

)

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

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);

The output is as follows:

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

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

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

)

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

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);

The output is as follows:

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

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

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

)

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:
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