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.
You can use json encode to do that. First you have to check if there is any fruits or not.
$fruits = isset($this->fruitcolor[$color]) ? $this->fruitcolor[$color] : array();
it's a shorthand version of if else statement which will return the fruits list if exists otherwise an empty array. Then you can encode that result using json_encode.
class FruitColor
{
private $fruitcolor;
function __construct($fruitcolor)
{
$this->fruitcolor = $fruitcolor;
}
public function getFruits($color)
{
$fruits = isset($this->fruitcolor[$color]) ? $this->fruitcolor[$color] : array();
return array('color' => $color, 'fruits' => $fruits);
}
public function getFruitsJSON($color){
return json_encode($this->getFruits($color));
}
}
$fruitcolor = new FruitColor(array(
"red" => array("apple", "strawberry"),
"yellow" => array("lemon", "ripe mango")
));
echo $fruitcolor->getFruitsJSON("red");
echo "\n";
echo $fruitcolor->getFruitsJSON("violet");
Here getFruits will return the data as php array and getFruitsJSON will return that array as JSON encoded string.
WARNING: using same classname to declare constructor is deprecated, use __construct
instead.
I'm not sure if I understand you. Normally what you do is going through serializer that serialize an object(in your case FruitColor) to some representation ex. JSON. What you want to do is that a method returns a json string which breaks SRP for sure but just for education let me help you with that.
class FruitColor
{
private $fruitColors;
public function __construct(array $fruitColors)
{
$this->fruitColors = $fruitColors;
}
public function getFruits(string $color): string
{
return json_encode([
'color' => $color,
'fruits' => $this->fruitColors[$color] ?? []
]);
}
}
$fruitcolor = new FruitColor([
"red" => ["apple", "strawberry"],
"yellow" => ["lemon", "ripe mango"],
]);
echo $fruitcolor->getFruits("red");
echo "\n";
echo $fruitcolor->getFruits("violet");
The other better approach would be not to use json_encode in getFruits but use it on an array that is returned by this method.
Something like so....
<?php
class FruitColor
{
private $fruitcolor = array ( );
public function __construct ( $fruitcolor )
{
$this->fruitcolor = $fruitcolor;
}
public function getFruits ( $color )
{
return json_encode ( array ( 'color' => $color, 'fruits' => array ( ( ! empty ( $this->fruitcolor[$color] ) ? $this->fruitcolor[$color] : '' ) ) ) );
}
}
$fruitcolor = new FruitColor ( array ( 'red' => array ( 'apple', 'strawberry' ), 'yellow' => array ( 'lemon', 'ripe mango' ) ) );
echo $fruitcolor->getFruits ( 'red' ) . "\r\n";
echo $fruitcolor->getFruits ( 'violet' ) . "\r\n";
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.