Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:49:47+00:00 2026-06-06T12:49:47+00:00

I’m working on a project, and I’ve come across an issue that has me

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:49:48+00:00Added an answer on June 6, 2026 at 12:49 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.