I want to display an output illustrated as json format but using PHP. I have an array for fruits and its color.
Array list
red = apple, strawberry
yellow = lemon, ripe mango
<?php
class FruitColor
{
private $fruitcolor;
function FruitColor($fruitcolor)
{
$this->fruitcolor = $fruitcolor;
}
public function getFruits($color)
{
return NULL;
}
}
$fruitcolor = new FruitColor(array(
"red" => array("apple", "strawberry"),
"yellow" => array("lemon", "ripe mango")
));
echo $fruitcolor->getFruits("red");
echo "\n";
echo $fruitcolor->getFruits("violet");
?>
As you can see, this line echo $fruitcolor->getFruits("red"); represents red
Where the output should be like this
{ "color":"red", "fruits": ["apple", "strawberry"]}
and if there's no color like violet in the array, the output should be like this
{ "color":"violet", "fruits":[] }
This way of coding like json is new to me. I guess i should use IF ELSE statement on this problem? But I dont know how can I implement it.