PHP - Create a recursive json array parse in JQuery
not sure if this can actually be done - OK probably can be done but beyond me. I have a php recursive array function like this:
function recursive_array($results) {
global $DBH;
if(count($results)) {
echo $res->Fname;
foreach($results as $res) {
$STH = $DBH->query("SELECT FID,FParentID,Fname FROM list WHERE FParentID = " . $res->FID ."" );
$fquerycount = $STH->rowCount();
$STH->setFetchMode(PDO::FETCH_OBJ);
recursive_array($STH);
}}}
which starts on the page like this:
$FID = isset($_GET['FID']) ? $_GET[' FID'] : 0;
$STH = $DBH->query("SELECT FID,FParentID,Fname FROM list WHERE FParentID ='0' " );
$STH->setFetchMode(PDO::FETCH_OBJ);
recursive_array($STH);
This function works well for me. BUT it is "plain" php. what I would like to do is to create a json array rather than echo
out the results. Then to parse the results through JQuery. My reason is I am using $.ge开发者_如何学编程tJSON('etc...')
for cross domain functionality with a "central" DB. OK I can use Iframes and just create "template" pages at the "central" domain, but I would prefer not to. (I just hate frames/Iframes)
Any suggestions / comments?
This is what I've done, actually in javascript:
function forall (obj, key, func) {
if (func) func(obj, key);
var item = obj[key];
if (item instanceof Object || item instanceof Array) {
for (var k in item) {
forall(item, k, func);
}
}
}
forall(obj, key, function(obj, key) {
console.log('object:'+ obj +'['+key+'] = '+ obj[key]);
});
Do this :
<?php
$json_results = array();
$json_results[] = $res->Fname;
.
.
.
//And after all iterated convert to json.
echo json_encode($json_results);
精彩评论