views:

312

answers:

2

i use $.getJSON to call php file. it echos this back.

for($i=0; $i<=10; $i++)
{
    $nr[] = $i;
}

echo "{'result': 'error', 'count': '$nr'}";

in jquery to get result i just use

alert(data.result)

how do i get all the nr:s in count?

EDIT: But how do i loop the array through in jquery? All the keys are numeric and i dont know how to retrieve them.

A: 

Use json_encode to convert your PHP array into an JSON array:

echo "{'result': 'error', 'count': ".json_encode($nr)."}";

You can even build your hole response message with native PHP types first and then convert it with json_encode:

$response = array(
    'result' => 'error',
    'count'  => array()
);
for ($i=0; $i<=10; $i++) {
    $response['count'][] = $i;
}
echo json_encode($response);
Gumbo
dont u mean json object? it seems like an object when you use the dots . to retrieve values:)
never_had_a_name
@fayer: The whole response value is an object with the members *result* and *count* where *result* is a string and *count* is an array. *result* can then be accessed with `data.result` and *count* with `data.count`.
Gumbo
A: 

... and you can loop through the values in jQuery with the .each() method.

TimS