Sunday 15 September 2013

php - json_encode - formatting issue? -



php - json_encode - formatting issue? -

i requesting output this:

response { error_num: 0 error_details: [ { "zipcode": 98119 }, { "zipcode": 98101 } ] }

the values irrelevant example.

my code looks this:

$returndata = array('error_num' => $error_code); $returndata['error_details'] = $error_msg; $temp_data = array(); $temp_value = ''; foreach ($zipcodes_array $value) { //$temp_data['zipcode'] = $value; //$temp_value .= json_encode($temp_data); $temp_value .= '{"zipcode":$value},'; } //$returndata['test'] = $temp_value; $returndata['zipcodes'] = $temp_value; echo json_encode($returndata);

my output varies depending on different attempts (which can see commented out things) basically, don't understand how 3rd part (the part zipcodes) doesn't have key or definition before first open bracket "["

here output code above:

{"error_num":0,"error_details":"","zipcodes":"{\"zipcode\":11111},{\"zipcode\":11112},{\"zipcode\":11113},{\"zipcode\":22222},{\"zipcode\":33333},{\"zipcode\":77325},{\"zipcode\":77338},{\"zipcode\":77339},{\"zipcode\":77345},{\"zipcode\":77346},{\"zipcode\":77347},{\"zipcode\":77396},{\"zipcode\":81501},{\"zipcode\":81502},{\"zipcode\":81503},{\"zipcode\":81504},{\"zipcode\":81505},{\"zipcode\":81506},{\"zipcode\":81507},{\"zipcode\":81508},{\"zipcode\":81509},"}

obviously did not show variables beingness filled/created because through mysql. values irrelevant. format of output i'm trying down. don't understand how have "zipcode": part in between {} brackets within section appears using json_encode

it close see how still has "zipcodes": part in there defining key values on? question is, "response" above beingness requested partner in json_encode format?? or in custom format i'll have create w/out using json features of php? can write based on way looks in illustration above (the response) thinking json_encode beingness used.

also, keeps putting \'s in front end of " isn't right either. know it's doing because i'm json_encode'ing string. see i'm trying do.

if custom stuff made resemble json, apologize. i've tried inquire partner guess i'm not asking right questions (or right person). can never seem give me answers.

edit: notice output doesn't have [ or ] in it. of test json_encode stuff has had in it. i'm sure me failing here cant figure out.

if want output json output @ top of question, write code this:

$returndata = array('error_num' => $error_code); $returndata['error_details'] = array(); foreach ($zipcodes_array $value) { $returndata['error_details'][] = array('zipcode' => (int)$value); } echo 'response ' . json_encode($returndata);

this homecoming json formatted requested above.

response { error_num: 0 error_details: [ { "zipcode": 98119 }, { "zipcode": 98101 } ] }

php json

No comments:

Post a Comment