PHP arrays
$grades = array( array( name => "tom", 
                  grade => 'A'
                ),
           array( name => "jeff", 
                  grade=> 'B'
                ),
           array( name => "lisa", 
                  grade => 'C'
                )
         );
$output=array
(  'status'=>'ok',
   'content'=>$grades
)
And the final JSON output I want to see would be {"status":'ok',"content":"{"tom":"A","jeff":"B","lisa":"B"}"}
The question is how should I manipulated the array so it can give me the final output like above JSON?
would json_encode($output); echo $output do the trick?
or
Do I have to do 
json_encode($grades)   then another json_encodes($output) but I am seeing extra \ thing like {"status":1,"content":"{\"tom\":\"A\",\"jeff\":\"B\",\"lisa\":\"B\"}"} 
Try this simple command
$my_array = [
    "test" => "value", 
    "another" => [
        "fruits" => ["Apple", "Orange"]
    ]
];
print_r(json_encode($my_array));
It will produce
{
    "test":"value",
    "another":{
        "fruits":["Apple","Orange"]
    }
}
                        I don't understand why you don't write just this:
<?php
$grades = array(
    array(
        'name' => 'tom',
        'grade' => 'A'
    ),
    array(
        'name' => 'jeff',
        'grade' => 'B'
    ),
    array(
        'name' => 'lisa',
        'grade' => 'C'
    ),
);
$output = array(
    'status'  => 'ok',
    'content' => $grades,
);
print_r(json_decode(json_encode($output)));
It prints:
stdClass Object
(
    [status] => ok
    [content] => Array
        (
            [0] => stdClass Object
                (
                    [name] => tom
                    [grade] => A
                )
            [1] => stdClass Object
                (
                    [name] => jeff
                    [grade] => B
                )
            [2] => stdClass Object
                (
                    [name] => lisa
                    [grade] => C
                )
        )
)
Isn't is the expected result?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With