简体   繁体   中英

Wrote 2 functions, don't understand what I did wrong

I've been practicing PHP more and more and am trying to do functions daily to learn from them.

Yesterday I wrote 2 functions but they completely didn't work and I was looking for help as to why!

My code:

<?php

function getFilesAndContent($path)
{
    $data[] = $fileData;

    $folderContents = new DirectoryIterator($path);

    foreach ($folderContents as $fileInfo) {
        if ($fileInfo->isDot()) {
            continue;
        }

        $fileData = [
            'file_name' => $fileInfo->getBasename(),
        ];

        if ($fileInfo->getExtension()) {
            $fileData['contents'] = getFileContents($fileInfo->getPathname());
        }

        $data = $fileData;
    }

    return $data;
}

function getFileContents($path)
{
    $names = file_get_contents($fileInfo->getPathname());

    $names = implode("\n", $names);

    sort($names);

    $contents = '';

    foreach ($names as $name) {
        $contents += $name . ' (' . strlen($name) . ')<br>';
    }

    return $contents;
}

foreach (getFilesAndContent('.') as $data) {
    echo $data['file_name'];
    echo '<br>';
    echo $data['contents'];

    echo '<hr>';
}

DISLCAIMER: I really would like to get these 2 functions to work BUT I already have a working alternative(thank you very much!) without any functions, this is meant as a learning opportunity for myself to improve, any help would be greatly appreciated!

You have several problems.

First, $data = $fileData; should be $data[] = $fileData; . Adding [] means that the assignment creates a new element in the array, rather than overwriting the entire variable. And when you initialize the variable at the beginning of getFilesAndContent , it should be $data = []; .

Second, file_get_contents($fileInfo->getPathname()) should be file_get_contents($path) . $fileInfo is a variable in getFilesAndContent , not getFileContents .

Third, implode() should be explode() . implode joins an array to create a string, explode() splits up a string into an array.

function getFilesAndContent($path)
{
    $data = [];
    $folderContents = new DirectoryIterator($path);
    foreach ($folderContents as $fileInfo) {
        if ($fileInfo->isDot()) {
            continue;
        }
        $fileData = ['file_name' => $fileInfo->getBasename(),];
        if ($fileInfo->getExtension()) {
            $fileData['contents'] = getFileContents($fileInfo->getPathname());
        }
        $data[] = $fileData;
    }
    return $data;
}

function getFileContents($path)
{
    $names = file_get_contents($path);
    $names = explode("\n", $names);
    sort($names);
    $contents = '';
    foreach ($names as $name) {
        $contents += $name . ' (' . strlen($name) . ')<br>';
    }
    return $contents;
}

foreach (getFilesAndContent('.') as $data) {
    echo $data['file_name'];
    echo '<br>';
    echo $data['contents'];
    echo '<hr>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM