I'm using this code to generate JSON string, but a faced with one problem, I try to find out is this the last element or not, I am using this if(end($aData) != $aD) but this is comparing the value, there is a problem, when in the array are same values, How could I compare the $keys(get the last key and compare with the current key), or get the last element.
$ret = '['; foreach($aDatas as $aData) { $ret .= "{"; foreach($aData as $key=>$aD) { $ret .= '"'.$key.'":"'.$aD.'"'; if(end($aData) != $aD) $ret .= ','; } $ret .= "}"; if(end($aDatas) != $aData) $ret .= ','; } echo $ret.']';
json_encode?json_encode()? php.net/json-encode ?