0

I want output in json like this

response:{url="www.google.com/raj.png", size=12.344KB},{url="www.google.com/raj2.png", size=12.344KB},{url="www.google.com/raj4.png", size=12.344KB}

But currently i am getting

"url=> www.google.com/img1.png size => 12.344 KB,url=> www.google.com/img2.png size => 12.344 KB"
1
  • 1
    Format your array in that case. Post your code where you encoding your json so that we may help you. Commented Mar 6, 2014 at 7:04

3 Answers 3

2
//Using some loop here
{
  $response[] = array('url' => 'url_value','size' => 'file_size');
}

//without loop hardcoded values:

$response = array ( array('url' => "www.google.com/raj.png",'size' => "12.344KB"),
array('url' => "www.google.com/img2.png",'size' => "10.344KB") );

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

Comments

0
$out= Array(
 Array('url'=>'www.goog', 'size'=>'12KB'),
 Array('url'=>'moogle', 'size'=>'13KB')
);
echo Json_encode($out);

Comments

0

Not sure how you are formatting your array, but if you define it like this it should do the trick:

$response_array = array(
  'response' => array(
    array(
      'url'  => 'www.google.com/raj.png',
      'size' => '12.344KB',
    ),
    array(
      'url'  => 'www.google.com/raj.png',
      'size' => '12.344KB',
    ),
    array(
      'url'  => 'www.google.com/raj.png',
      'size' => '12.344KB',
    ),
  ),
);

$respose_json = json_encode($response_array);
echo($response_json);

For looping through some results, try something like this:

$response_array = array('response' => array());
foreach($result_set as $result_item) {
  $response_item = array();
  $response_item['url'] = $result_item['url'];
  $response_item['size'] = $result_item['size'];
  $response_array['response'][] = $response_item;
}

$respose_json = json_encode($response_array);
echo($response_json);

With the $result_set above being whatever data you've pulled from a DB, server, etc.

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.