Reputation: 151
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());
Upvotes: 1
Views: 101
Reputation: 4907
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;
}
Upvotes: 1
Reputation:
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
)
)
Upvotes: 2