首页 > 解决方案 > Can't get an array returning from a class function

问题描述

I have a PHP class that represents 'cars', inside of it I've tried to create a recursive function listRedCars() that returns an array with all red cars of that class property $cars.

The issue I'm having here is that, when I try to see the listRedCars() returning array, by using print_r($my_cars->listRedCars()) at the end of my code, it shows nothing. Meanwhile, inside of the recursive function listRedCars() if I replace the return for print_r($this->red_cars), it displays the array normally.

I've forgot to inform before so I'm editting this now: This is actually a challenge where I'm supposed to use listRedCars() as a recursive function.

I can't find what I am doing wrong, and I really aprecciate if someone could help me here.


    <?php
    
        class Car {
    
            public $year;
            public $brand;
            public $model;
            public $color;
    
            public function addCar($_year, $_brand, $_model, $_color) {
    
                $this->year = $_year;
                $this->brand = $_brand;
                $this->model = $_model;
                $this->color = $_color;
    
            }
    
        }
    
        class Controller {
            // Mockup
        }
    
        class Cars extends Controller {
    
            public $cars = array();        
            public $red_cars = array();
    
            public function setCars($array_cars) {
    
                foreach ($array_cars as $c) {
                    $this->cars[] = $c;
                }
                
            }
    
            public function getcars() {
                return $this->cars; 
            }
    
            public function listRedCars(int $index = 1) {
    
                if ($index < sizeof($this->cars)) {
    
                    if ($this->cars[$index]->color == "Red") {
                        $this->red_cars[] = $this->cars[$index];
                    }
                    
                    $this->listRedCars($index + 1);
    
                } else {     
                    // print_r($this->red_cars);               
                    return $this->red_cars;                
                }
            
            }
    
        }
    
        
        $car_list = array();
    
        $new_car = new Car();
        $new_car->addCar(1988, "Ford", "Corcel", "Gray");
        $car_list[] = $new_car;
    
        $new_car = new Car();
        $new_car->addCar(2015, "Fiat", "Palio", "Red");
        $car_list[] = $new_car;
    
        $new_car = new Car();
        $new_car->addCar(1985, "Chevrolet", "Opala", "Black");
        $car_list[] = $new_car;
    
        $new_car = new Car();
        $new_car->addCar(2016, "Ford", "Fusion", "Red");
        $car_list[] = $new_car;
    
        $new_car = new Car();
        $new_car->addCar(2008, "Volkswagen", "Golf", "White");
        $car_list[] = $new_car;    
    
    
        $my_cars = new Cars();
        $my_cars->setCars($car_list);    
    
        print_r($my_cars->listRedCars());

标签: phparrays

解决方案


You can use array_filter() to easily filter out red cars:

public function listRedCars() {
    return array_filter($this->cars, function($car) {
        return $car->color == 'Red';
    });
}

print_r($my_cars->listRedCars()); will output

Array
(
    [1] => Car Object
        (
            [year] => 2015
            [brand] => Fiat
            [model] => Palio
            [color] => Red
        )
    [3] => Car Object
        (
            [year] => 2016
            [brand] => Ford
            [model] => Fusion
            [color] => Red
        )
)

推荐阅读