0

Sorry if this seems obvious but all the searches I am doing for this are returning complex answers and I have been struggling with this all day... I have been trying to create json data in PHP and really I want to send variables into the json data. I have not been able to do that so I followed a tutorial online and copied the code exactly yet i'm still not getting an output in my local host.. Please can somebody put me out of my misery...

  <?php

$jsonData = new stdClass();
$people = array(

                array(
                    'name' => 'Luci',
                     'age' => 25,
                      'sex' => 'female'
                ),
                array(
                    'name' => 'John',
                     'age' => 27,
                      'sex' => 'male')
                ),
                array(
                    'name' => 'Peter',
                     'age' =>  22,)
                      'sex' => 'male'
                )
            );



$jsonData->source = "Program Knowledge";
$jsonData->published = date('Y-m-d H:s:i;');
$jsonData->status = true;
$jsonData->people = $people;
echo json_encode($jsonData);



?>

eventually I was going to try

$var = 123456;
$jsonData->codeid = $var

but I need to figure out just how to get it so work with data I have typed in.

Many thanks..!!

6
  • Looks correct. what's the problem? Commented May 15, 2015 at 16:48
  • I get no output at all from the echo json_encode($jsonData); not sure as to why... Thx Commented May 15, 2015 at 16:50
  • have you run php lint (php -l)? You have an extra parenthesis after John 'sex => male' and Peter 'age => 22'. Also, did you forget the semicolon after $jsonData->codeid = $var ? Commented May 15, 2015 at 16:59
  • ok it was simply the parenthesis issue.. It's been a long day... Thanks !! Commented May 15, 2015 at 17:07
  • while developing/debugging, it may be worthwhile to output errors.. error_reporting(E_ALL); ini_set('display_errors', true); php.net/manual/en/function.error-reporting.php php.net/manual/en/function.ini-set.php Commented May 15, 2015 at 17:09

1 Answer 1

2
                  'sex' => 'male')
                 'age' =>  22,)

Need to remove the parenthesis from these two lines, they're parsing errors

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

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.