1

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

3
  • 2
    Make it two methods, one to fill the red cars array, one to get it/output it. Commented Dec 19, 2020 at 5:25
  • I'll try that, thanks. Commented Dec 19, 2020 at 5:29
  • Thanks, but as I wrote below, I need listRedCars() to be a recursive function anyhow. Commented Dec 19, 2020 at 10:53

2 Answers 2

2

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
        )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works correctly. Problem is, this is a challenge where I'm supposed to use listRedCars() as a recursive function. Most of all I want to understand why that returning array don't show nothing and print_r inside the function show the red cars. Nevertheless, thank you for your response, I didn't knew about array_filter.
Oh ok, didn't know it had to be a recursive method, wasn't clear from your question, you might want to add that info to your question in case other people show up to help
Sure, my mistake.
1

A couple of minor changes are needed to make it work with the current code:

First, inside the foreach in setCars(), change:

$this->cars = $c;

to:

$this->cars[] = $c;

The recursion function parameter needs to change from:

public function listRedCars(int $index = 1) {

to:

public function listRedCars(int $index = 0) { // Array indexes start at 0

Then inside the recursion, change:

$this->listRedCars($index + 1);

to:

return $this->listRedCars($index + 1);

Then it all works as expected.

Full code:

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 = 0) {

        if ($index < sizeof($this->cars)) {
             if ($this->cars[$index]->color == "Red") {
                $this->red_cars[] = $this->cars[$index];
            }
            
            return $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());  

Output:

Array
(
    [0] => Car Object
        (
            [year] => 2015
            [brand] => Fiat
            [model] => Palio
            [color] => Red
        )

    [1] => Car Object
        (
            [year] => 2016
            [brand] => Ford
            [model] => Fusion
            [color] => Red
        )
)

Side note: Unless it is a specific requirement, know that instead of using a loop inside the setCars function, the handling can be simplified to:

public function setCars($array_cars) {

   //foreach ($array_cars as $c) {
   //     $this->cars[] = $c;
   //}

   $this->cars = $array_cars;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.