0

I need to create data as this:

{v: 1.0, f: '<h1>1.0</h1>'}

...

but now I only know how to get first parametr so I get:{v: 1.0},

with this code:

 $temp[] = array('v' => (string) $naziv);

what I need to add to my code: $temp[] = array('v' => (string) $naziv); to get like this json: {v: 1.0, f: '<h1>1.0</h1>'}

3
  • 2
    Your desired data does not conform to JSON standards. Property names are in quotes. Commented Jun 5, 2014 at 15:25
  • 1
    Your target should be {"v": "1.0", "f": "<h1>1.0</h1>"} Commented Jun 5, 2014 at 15:27
  • 2
    Don't build your own JSON in PHP. Use json_encode. It will build it correctly if you give it an object: php.net/manual/en/function.json-encode.php Commented Jun 5, 2014 at 15:28

2 Answers 2

2
$naziv = 1.0;
$header ='<h1>1.0</h1>';

$temp[] = array('v' => (string) $naziv,
                'f' => (string) $header
          );

echo json_encode($temp);

or

$naziv = 1.0;
$header ='<h1>1.0</h1>';

$temp['v'] = $naziv;
$temp['f'] = $header;

echo json_encode($temp);
Sign up to request clarification or add additional context in comments.

Comments

1
<?php

$naziv = 1.0;

$temp = array(
    "v" => $naziv
    "f" => "<h1>{$naziv}</h1>",
);

echo json_encode($temp);

This will result in

{"v":1.0,"f":"<h1>1.0</h1>"}

1 Comment

this give me an error when I try to write this: $temp[] = array('v' => (string) $naziv, 'f': '<h1>'.$naziv.'</h1>');

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.