-3

I want to build a json object with PHP. The json object needs to be like this (for creating a google line charts):

{"cols":[{"id":"bingo","label":"bingo","type":"string"},{"id":"value","label":"value","type":"number"}],"rows":[{"c":[{"v":"date1"},{"v":151}]},{"c":[{"v":"date2"},{"v":102}]},{"c":[{"v":"date3"},{"v":52}]},{"c":[{"v":"date4"},{"v":32}]},{"c":[{"v":"date5"},{"v":7}]},{"c":[{"v":"date5"},{"v":7}]},{"c":[{"v":"date5"},{"v":7}]}]}

I have a problem creating the following part:

{"c":[{"v":"date3"},{"v":52}]},{"c":[{"v":"date4"},{"v":32}]},{"c":[{"v":"date5"},{"v":7}]},{"c":[{"v":"date5"},{"v":7}]},{"c":[{"v":"date5"},{"v":7}]}

How can I create this with PHP?

1
  • 4
    What problem are you having creating it? What does your original data look like? Commented Nov 24, 2011 at 16:09

2 Answers 2

1

PHP comes with a JSON extension. The only thing you will need to figure out is how you have to create the data in PHP. [] in JSON is an array, {} is a dictionary. For the part mentioned you'll need something like this:

$foo = array(
   array(
      'c' => array(
         array('v' => 'date3'),
         array('v' => 52)
      )
   ),
   array(
      'c' => array(
         array('v' => 'date4'),
         array('v' => 32)
      )
   ),
   // and so on...
);
Sign up to request clarification or add additional context in comments.

Comments

1

To recreate this in PHP use:

$data = array (
  'cols' => 
  array (
    array (
      'id' => 'bingo',
      'label' => 'bingo',
      'type' => 'string',
    ),
    array (
      'id' => 'value',
      'label' => 'value',
      'type' => 'number',
    ),
  ),
  'rows' => 
  array (
    array (
      'c' => 
      array (
        array (
          'v' => 'date1',
        ),
        array (
          'v' => 151,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date2',
        ),
        array (
          'v' => 102,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date3',
        ),
        array (
          'v' => 52,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date4',
        ),
        array (
          'v' => 32,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date5',
        ),
        array (
          'v' => 7,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date5',
        ),
        array (
          'v' => 7,
        ),
      ),
    ),
    array (
      'c' => 
      array (
        array (
          'v' => 'date5',
        ),
        array (
          'v' => 7,
        ),
      ),
    ),
  ),
);

Now if you want to print it in JSON, use:

echo json_encode ($data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.