Eric Felder
Eric Felder

Reputation: 57

Accessing class instances in an array

I'm working on a project, and I've come across an issue that has me stumped. The code below is a class file and a test page to make sure it's working. It's for somebody else who is programming the site, otherwise I would code the JSON output differently. Basically, the person implementing it just has to pull a bunch of data (like below) from a database, and loop through, instantiating a class object for each result, and plugging each instance into an array, and passing the array to the printJson function, which will print the JSON string. Here is what I have:

Results.php

    <?php

    class Result
    {
        public $Category = NULL;
        public $Title = NULL;
        public $Price = NULL;

        public function __construct($category, $title, $price)
        {
            $this->Category = $category;
            $this->Title = $title;
            $this->Price = $price;
        }

        public static function printJson($arrayOfResults)
        {
            $output = '{"results": [';

            foreach ($arrayOfResults as $result)
            {
                $output += '{"category": "' . $result->Category . '",';
                $output += '"title": "' . $result->Title . '",';
                $output += '"price": "' . $result->Price . '",';
                $output += '},';
            }

            $output = substr($output, 0, -1);
            $output += ']}';

            return $output;
        }
    }

    ?>

getResults.php

    <?php

    require_once('Result.php');

    $res1 = new Result('food', 'Chicken Fingers', 5.95);
    $res2 = new Result('food', 'Hamburger', 5.95);
    $res3 = new Result('drink', 'Coke', 1);
    $res4 = new Result('drink', 'Coffee', 2);
    $res5 = new Result('food', 'Cheeseburger', 6.95);

    $x = $_GET['x'];

    if ($x == 1)
    {
        $array = array($res1);
        echo Result::printJson($array);
    }
    if ($x == 2)
    {
        $array = array($res1, $res2);
        echo Result::printJson($array);
    }
    if ($x == 3)
    {
        $array = array($res1, $res2, $res3);
        echo Result::printJson($array);
    }
    if ($x == 5)
    {
        $array = array($res1, $res2, $res3, $res4, $res5);
        echo Result::printJson($array);
    }

    ?>

The end result is if I go to getResults.php?x=5, it will return $res1 thru $res5 (again, this is just to test, I would never do something like this in production) formatted as JSON. Right now, I get '0' outputted and I cannot for the life of me figure out why. Could my foreach loop not be written properly? Please, any help you could provide would be awesome!

Upvotes: 2

Views: 88

Answers (1)

cmbuckley
cmbuckley

Reputation: 42458

It's because you're using + for concatenation rather than .:

$output .= '{"category": "' . $result->Category . '",';
$output .= '"title": "' . $result->Title . '",';
$output .= '"price": "' . $result->Price . '",';
$output .= '},';

But you should really not construct the JSON yourself, as it leads to a number of errors making for invalid JSON (trailing commas etc). Use something like this instead:

public static function printJson(array $arrayOfResults)
{
    $results['results'] = array_map('get_object_vars', $arrayOfResults);

    return json_encode($results);
}

Upvotes: 2

Related Questions