Skip to content

Instantly share code, notes, and snippets.

@JoshyFrancis
Last active May 22, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshyFrancis/5ae379f1f279d384f5d50ab0021835d4 to your computer and use it in GitHub Desktop.
Save JoshyFrancis/5ae379f1f279d384f5d50ab0021835d4 to your computer and use it in GitHub Desktop.
Parse Javascript array which is not in JSON fromat, into PHP array.
function parse_javascript_array($s_arr,$date_format='Y-m-d H:i:s'){
$sample_s_arr='
[{
Customer_Name: "Opening Balance",
DebitAmount: "0.00",
CreditAmount: 0.00,
BalanceAmount: "0.00"
}, {
ID: 890,
Remarks: "Invoice No : 1 , Date : 2018-01-11 09:51:00",
InvNo: "1",
InvDate: "2018-01-11 09:51:00",
Amount: "-343.00",
Customer_Name: "Customer",
DebitAmount: 0.00,
CreditAmount: "343.00",
BalanceAmount: "-343.00"
}, {
totalrecords: 2,
processing_time: 0.0018410682678223,
DebitAmount: "343.00",
CreditAmount: "343.00",
BalanceAmount: "<b>343.00 Cr</b>",
details:[{
Location: "Address"
}
]
}]
';
if($s_arr==''){
$s_arr=$sample_s_arr;
}
$out=[];
$s_arr= trim($s_arr) ;
$s_arr_len=strlen($s_arr);
$token='';
$last_token='';
$last_chr='';
$double_quote_open=false;
$new_element=true;
$new_member=false;
$array_level=-1;
$a_idx=-1;
for($i=0;$i<$s_arr_len ;$i++){
$chr=substr($s_arr,$i ,1);
check_again:
switch($chr){
case chr(10):case chr(13):
if($double_quote_open==true){
goto next;
}
if($array_level>0){
goto next;
}
$chr='';
break;
case '\\':
if($double_quote_open==true){
$last_chr='\\';
}
break;
case '"': // "
if($last_chr=='\\'){
$last_chr='';
goto next;
}
$double_quote_open=!$double_quote_open;
break;
case '[': case '{':
if($double_quote_open==true){
goto next;
}
$array_level+=1;
if($array_level==0){
$chr='';
}
break;
case ']': case '}':
if($double_quote_open==true){
goto next;
}
if($array_level==0){
$chr=',';
goto check_again;
}else{
if(($array_level)==1){
$token.=$chr;
if($last_token!=''){
$out[$last_token]=parse_javascript_array($token,$date_format );
}else{
if($new_element ){
$out[]=[];
$new_element=false;
}
$a_idx+=1;
$out[$a_idx] =parse_javascript_array($token,$date_format ) ;
}
$token='';
$chr='';
$last_token='';
}
}
$array_level-=1;
break;
case ':':
if($double_quote_open==true){
goto next;
}
if($array_level>0){
goto next;
}
if(substr($token,0,1)=='"'){
$token =substr($token ,1);
}
if(substr($token,strlen($token)-1,1)=='"'){
$token =substr($token ,0,strlen($token)-1);
}
$last_token=$token;
if($last_token!=''){
//if($new_element){# && !isset($out[$a_idx]) || !array_key_exists ($a_idx,$out )){
if($new_element && $new_member){
$out[]=[];
$a_idx+=1;
$new_element=false;
}
if( $new_member ){
$out[$a_idx][$last_token]='';
}else{
$out[$last_token]='';
$new_element=false;
}
}
$token='';
$chr='';
goto next;
break;
case ',':
if($double_quote_open==true){
goto next;
}
if($array_level>0){
goto next;
}
#$token=trim($token);
$s=false;
if(substr($token,0,1)=='"'){
$token =substr($token ,1);
$s=true;
}
if(substr($token,strlen($token)-1,1)=='"'){
$token =substr($token ,0,strlen($token)-1);
$s=true;
}
$d=date_create_from_format( $date_format,$token);
if($d!==false){
$token=$d;
}else if($s==false){
if(is_numeric($token)){
if(strpos($token,'.')!==false){
$token=floatval($token);
}else{
$token=intval($token);
}
}else{
if($token=='true' ){
$token=true;
}else if($token=='false' ){
$token=false;
}
}
}
if($last_token!=''){
if($a_idx!=-1 && array_key_exists ($last_token, $out[$a_idx])){
$out[$a_idx][$last_token]=$token;
}else{
$out[$last_token]=$token;
}
$new_member=false;
$token='';
}
if($token!=''){
if($new_element){
$out[]=[];
$new_element=false;
}
$a_idx+=1;
$out[$a_idx]=$token;
}
$last_token='';
$token='';
$chr='';
goto next;
break;
case ' ': case chr(9): # chr(32): tab:
if($double_quote_open==true){
goto next;
}
if($array_level>0){
goto next;
}
$chr='';
break;
}
next:
$token.=$chr;
}
return $out;
/*
* All honour and glory and thanks and praise and worship belong to him, my Lord Jesus Christ.
* All honour and glory and thanks and praise and worship belong to him, my Lord Jesus Christ.
* All honour and glory and thanks and praise and worship belong to him, my Lord Jesus Christ.
*/
}/*parse_javascript_array*/
//var_dump(json_decode('{selected:false,disabled:false}'));// null
//var_dump(parse_javascript_array('{selected:false,disabled:false}')); // an array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment