So erhalten Sie einen Wert aus dem wiederholten Aufrufen einer Funktion mit unterschiedlichen Parametern in PHP
P粉578680675
P粉578680675 2024-04-03 16:49:35
0
1
578

Das ist mein Ergebnis, aber das ist nicht meine Erwartung

Name : YOUR_NAME
Lesson : Algorithm and Programming
Total duration : 3
Room : A17
Lecturer : Ikhsan Permadi
Day : Monday, Thursday
Building : South Block

Name : YOUR_NAME
Lesson : Data Structure 2
Total duration : 2
Room : B10
Lecturer : Faisal Sulhan
Day : Wednesday
Building : North Block

Name : YOUR_NAME
Lesson : Data Structure 2
Total duration : 2
Room : B10
Lecturer : Faisal Sulhan
Day : Wednesday
Building : North Block

Ich kann in der Klasse keine Array-Werte erstellen. Wie kann ich das tun? Dies ist meine erwartete Ausgabe

// Output
// ================================================

// Name: YOUR_NAME
// Lesson: Algorithm and Programming
// Total duration: 3 SKS
// Room: A17
// Lecturer: Ikhsan Permadi
// Day: Monday, Thursday
// Building: South Block


// Name: YOUR_NAME
// Lesson: Artificial Intelligence, Data Structure 2
// Total duration: 5 SKS
// Room: B12, B10
// Lecturer: Ikhsan Permadi, Faisal Sulhan
// Day: Thursday, Wednesday
// Building: North Block


// Name: YOUR_NAME
// Lesson: Algorithm and Programming, Advanced Database, Artificial Intelligence, Data Structure 2
// Total duration: 12 SKS
// Room: A17, B12, B10
// Lecturer: Ikhsan Permadi, Faisal Sulhan
// Day: Monday, Thursday, Tuesday, Wednesday
// Building: South Block, North Block
$student = new Student;
$student->takeLesson('Algorithm and Programming');

$student->getLessonSummary();

$student = new Student;
$student->takeLesson('Artificial Intelligence');
$student->takeLesson('Data Structure 2');

$student->getLessonSummary();

$student = new Student;
$student->takeLesson('Algorithm and Programming');
$student->takeLesson('Advanced Database');
$student->takeLesson('Artificial Intelligence');
$student->takeLesson('Data Structure 2');

$student->getLessonSummary();

class Lesson
{   
    public function getDetail($lessonName)
    {
        $details = [
            [
                'name' => 'Algorithm and Programming',
                'lecturer' => 'Ikhsan Permadi',
                'room' => 'A17',
                'schedule' => [
                    [
                        'day'=> 'Monday',
                        'duration'=> '2 SKS'
                    ],
                    [
                        'day'=> 'Thursday',
                        'duration'=> '1 SKS'
                    ]
                ]
            ],
            [
                'name' => 'Advanced Database',
                'lecturer' => 'Faisal Sulhan',
                'room' => 'B12',
                'schedule' => [
                    [
                        'day'=> 'Tuesday',
                        'duration'=> '2 SKS'
                    ],
                    [
                        'day'=> 'Thursday',
                        'duration'=> '2 SKS'
                    ]
                ]
            ],
            [
                'name' => 'Artificial Intelligence',
                'lecturer' => 'Ikhsan Permadi',
                'room' => 'B12',
                'schedule' => [
                    [
                        'day'=> 'Thursday',
                        'duration'=> '3 SKS'
                    ]
                ]
            ],
            [
                'name' => 'Data Structure 2',
                'lecturer' => 'Faisal Sulhan',
                'room' => 'B10',
                'schedule' => [
                    [
                        'day'=> 'Wednesday',
                        'duration'=> '2 SKS'
                    ]
                ]
            ],
        ];
        foreach ($details as $key => $detail) {
            if ($detail['name'] == $lessonName) {
                return $details[$key];
            }
        }
    }
}

class Room
{
    public function getBuilding($roomName)
    {
        $details = [
            'A17' => 'South Block',
            'B12' => 'North Block',
            'B10' => 'North Block'
        ];

        // sleep(2);

        return $details[$roomName];
    }
}

   
class Student
{
    public function takeLesson($lessonName)
    {   
        $lesson = new Lesson;
        $lesson = $lesson->getDetail($lessonName);

        
        $dataLesson = $lesson['name'];        
        
       
        $totalDuration = array();
        $days = array();
        foreach($lesson['schedule'] as $schedule){
            $totalDuration[] = $schedule['duration'];
            $days[] = $schedule['day'];
        }

        $day = implode(", ", $days);

        $totalDuration = array_sum($totalDuration);

        $building = new Room;
        $building = $building->getBuilding($lesson['room']);
       
        $this->data = [
            "Name" => "YOUR_NAME",
            "Lesson" => $dataLesson,
            "Total duration"=> $totalDuration,
            "Room" => $lesson['room'],
            "Lecturer" => $lesson['lecturer'],
            "Day" => $day,
            "Building" => $building           
            ];
        
    }

    public function getLessonSummary()
    {
        $data = $this->data;
        list() = multiple();

        foreach ($data as $key => $value) {
            if("array" == gettype($value)){
                $value= json_encode($value);
            }
            echo "$key : $value\n";
            echo '<br>';
        }      
        echo '<br>';
        

    }
}

P粉578680675
P粉578680675

Antworte allen(1)
P粉166675898

问题出在您的学生班级。

每次调用 takeLesson 时,您都会覆盖 data 属性

所以可以通过使用数组来存储它们来解决 像这样

class Student
{
    private $data = [];

    public function takeLesson($lessonName)
    {   
        $lesson = new Lesson;
        $lesson = $lesson->getDetail($lessonName);

        
        $dataLesson = $lesson['name'];        
        
       
        $totalDuration = array();
        $days = array();
        foreach($lesson['schedule'] as $schedule){
            $totalDuration[] = $schedule['duration'];
            $days[] = $schedule['day'];
        }

        $day = implode(", ", $days);

        $totalDuration = array_sum($totalDuration);

        $building = new Room;
        $building = $building->getBuilding($lesson['room']);
       
        $this->data[] = [
            "Name" => "YOUR_NAME",
            "Lesson" => $dataLesson,
            "Total duration"=> $totalDuration,
            "Room" => $lesson['room'],
            "Lecturer" => $lesson['lecturer'],
            "Day" => $day,
            "Building" => $building           
            ];
        
    }

    public function getLessonSummary()
    {
        $dataArray = $this->data;
        list() = multiple();

        foreach($dataArray as $data) {
            foreach ($data as $key => $value) {
                if("array" == gettype($value)){
                    $value= json_encode($value);
                }
                echo "$key : $value\n";
                echo '
'; } echo '
'; } } }
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage