Viewed   59 times

Solr returns response in following JSON format.



What will be the simple way to read student_id, student_name using PHP?



Use $obj = json_decode($yourJSONString); to convert it to an object.

Then use foreach($obj->response->docs as $doc) to iterate over the "docs".

You can then access the fields using $doc->student_id and $doc->student_name[0].

Thursday, November 3, 2022

Doctypes belong on HTML documents, not JSON.

Try something like this in your PHP file (and only this)

header('Content-Type: application/json');

Given what you have posted, I can't see any reason to even involve PHP. I'm guessing you've only posted a very simple example. Should it become more complex, involving server-side processing, data retrieval, etc, use PHP's json_encode(), for example

header('Content-Type: application/json');
$data = array(
    'Id'  => 0,
    'foo' => $someOtherComplexVariable
echo json_encode($data);
Wednesday, September 21, 2022

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022

Finally I Solve The Problem. Here is My Solution

        $jsonObject = file_get_contents("");
    $jsonres = json_decode($jsonObject,true);               
        $val = $jsonres['Data'];      
$phpArray = json_decode($val, true);
foreach ($phpArray as $key => $value) {
    foreach ($value as $k => $v) { 
        if($k === 'CurrencyDescription'){ 

    echo '<option value=' . $curName. '>' . $curName. '</option>'; 

Tuesday, September 6, 2022

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
    echo "true";    
else {
    echo "false";   
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :