0

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.

2
  • php.net/manual/en/function.json-encode.php??? Commented Feb 14, 2019 at 18:14
  • hello sir I tried to put this echo json_encode($fruitcolor); to getFruits function it returns "null null" Commented Feb 14, 2019 at 18:22

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

7 Comments

Short note: coalescing operator (??) introduced in PHP 7.0 . It might throw an error if someone tried to run this on php 5x.
This works fine to me. Sir lfty maybe got a point when the codes tried to run in older version can appear a problem. my knowledge in php not that deep but how can I avoid that error in the future?
'fruits' => isset($this->fruitColors[$color]) ? $this->fruitColors[$color] : []
There is no problem if you run php 7. Some shared hosting still doesn't support php7. In that case they need to do this using the old fashioned way ( isset ). If you are running php7, its fine.
PHP 5.x is not supported anymore even 7.0 is not supported. php.net/supported-versions.php If you are on a hosting that doesn't support it I would run away from that hosting because it's just not secure
|
0

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";


 ?>

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.