note 105683 deleted from function.json-encode by cmb
| From: | cmb@php.net | Date: | Tue, 05 Nov 2019 11:38:34 +0000 |
| Subject: | note 105683 deleted from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212923@lists.php.net to get a copy of this message | ||
Note Submitter: grkworld1 at yahoo dot co dot in
----
copy the php tagged code in a page
this is use full for multy dimention array
<?php
function arr_2_str($arr,$counter=1,$str="")
{
foreach( $arr as $key=>$value)
{
if(is_array($value))
{
$str.=
$key."=$counter>".arr_2_str($value,($counter+1))."=".$counter.">~Y~|".$counter."|";
}
else
{
$str.=$key."=$counter>".$value."|$counter|";
}
}
return rtrim($str,"|$counter|");
}
function str_2_arr($str,$counter=1,$arr=array(),$temparr=array())
{
$temparr=explode("|$counter|",$str);
foreach( $temparr as $key=>$value)
{
$t1=explode("=$counter>",$value);
$kk=$t1[0];
$vv=$t1[1];
if ($t1[2]=="~Y~")
{
$arr[$kk]=str_2_arr($vv,($counter+1));
}
else
{
$arr[$kk]=$vv;
}
}
return $arr;
}
$arr=array();
$arr[1]="a";
$arr[2][1]="b";
$arr[2][2]="c";
$arr[2][3][1]="d";
$arr[2][3][2][1]="e1";
$arr[2][3][2][2]="e2";
$arr[2][3][3]="f";
print "<pre>";
print_r($arr);
print "<br><br><br>";
print $ssttrr=arr_2_str($arr);
print "<br><br><br>";
print_r(str_2_arr($ssttrr));
/*print "<br><br><br>";
print "use of json";
print "<br><br><br>";
print $sstr=json_encode($arr);
print "<br><br><br>";
print_r(json_decode($sstr));*/
print "</pre>";
?>