Remove null array before correct json in php -


i want echo php array in json format in back-end. when print it, shows

[][][][][][][][][][][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][][][][][][][][][][] [][][][][][][][][][][][][][][][][][][][][][][][][] [][][][ { "user":"root", "pid":"31692", "ppid":"31691", "elapsed":"00:05", "vsz":"1072408", "%cpu":"6.2", "state":"s", "command":"\/opt\/iims_client\/utils\/nfstop --rpc --interval 60 --sample 2 --mount all\n" },{ "user":"ctxsrvr", "pid":"29309", "ppid":"5152", "elapsed":"9-07:51:37", "vsz":"605648", "%cpu":"0.3", "state":"s", "command":"\/opt\/citrix\/vda\/bin\/ctxgfx -session 87\n" }] 

, not need null array before "user", how can remove them? thanks. here code:

elseif(preg_match("/<toptenproc>/",$line)){          $line=fgets($file,1024);         $line=ltrim($line);         $header=preg_split('/\s+/',$line);         while(!((preg_match("/<\/toptenproc>/",$line)))){             $line=fgets($file,1024);             $line=ltrim($line);             $vars=preg_split('/\s+/',$line,8);             // echo $vars[5];             array_push($result, array($header[0] => $vars[0], $header[1] => $vars[1], $header[2] => $vars[2], $header[3] => $vars[3], $header[4] => $vars[4], $header[5] => $vars[5], $header[6] => $vars[6], $header[7] => $vars[7]));         }      // $result = (object) array_filter((array) $result);     echo json_encode($result); 

your first regular expression checks <toptenproc>
second regular expression checks </toptenproc>

if ending tag hasn't been encountered yet, shouldn't echoing anything. solution should simple checking array size before echoing anything:

if(count($result)) {     echo json_encode($result); } 

alternative, can put $whileloopran = false; before loop,
put $whileloopran = true; inside loop , preform echo when loop ran. people prefer $i++; use "i" instead, whatever floats boat.

it's possible, you're echoing , should outside elseif, after loop finishes.

if you're trying parse xml, should using xml parser anyways, not regexp.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -