php - 为 ExtJS 树控件准备多维数组

标签 php javascript extjs

请不要将此问题视为重复问题而关闭............ 我是 php 新手。 我正在 extjs 中开发一个树网格。我需要以树格式显示字段。所以对于前端,我需要发送编码数据。 我想要两个函数来编码和解码字符串变量、多维数组、带有一组分隔符的变量。 例如,如果我有一个数组............

array(
array( "task" => "rose",
"duration" => 1.25,
"user" => 15
),
array( "task" => "daisy",
"duration" => 0.75,
"user" => 25,
),
array( "task" => "orchid",
"duration" => 1.15,
"user" => 7
),
array( "task" => "sunflower",
"duration" => 1.50,
"user" => 70
)

);

我想将所有数组字段或单个字段编码为......

array(
array( "task" => "rose",
"duration" => 1.25,
"user" => 15
),
array( "task" => "daisy",
"duration" => 0.75,
"user" => 25$sbaa,
),
array( "task" => "orchid",
"duration" => 1.15,
"user" => 7$!ass,
),
array( "task" => "sunflower",
"duration" => 1.50,
"user" => 70$!abc
)

);

所以像这样我只需要编码字符串,带有分隔符的变量......... 稍后,所有编码值在返回后端之前都将被解码......为此,我必须使用这个插件............ 编码.class.php......................

<?php

/*-------------------------
Author: Jonathan Pulice
Date: July 26th, 2005
Name: JPEncodeClass v1
Desc: Encoder and decoder using patterns.
-------------------------*/

class Protector
{

var $Pattern = "";
var $PatternFlip = "";
var $ToEncode = "";
var $ToDecode = "";
var $Decoded = "";
var $Encoded = "";
var $Bug = false;
var $DecodePattern = "";

function Debug($on = true)
{
$this->Bug = $on;
}

function Encode()
{


$ar = explode(":", $this->Pattern);
$enc = $this->ToEncode;

if ($this->Bug) echo "<!-- BEGIN ENCODING -->\n";

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$enc = base64_encode($enc);
break;
case "D":
$enc = base64_decode($enc);
break;
case "R":
$enc = strrev($enc);
break;
case "I":
$enc = $this->InvertCase($enc);
break;
}
if ($this->Bug) echo "<!-- {$ltr}: {$enc} -->\n";
}

if ($this->Bug) echo "<!-------------------->\n\n";

@$this->Encoded = ($enc == $this->Str) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $enc;

return $this->Encoded;

}

function Decode()
{

$pattern = ($this->DecodePattern != "") ? $this->DecodePattern : $this->Pattern;

//Reverse the pattern
$this->PatternFlip($pattern);

//make into an array
$ar = explode(":", $this->PatternFlip);

$t = ($this->Encoded == "") ? $this->ToDecode : $this->Encoded;

if ($this->Bug) echo "<!-- BEGIN DECODING -->\n";

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$t = base64_encode($t);
break;
case "D":
$t = base64_decode($t);
break;
case "R":
$t = strrev($t);
break;
case "I":
$t = $this->InvertCase($t);
break;
}
if ($this->Bug) echo "<!-- {$ltr}: {$t} -->\n";
}

if ($this->Bug) echo "<!-------------------->\n\n";

$this->Decoded = ($t == $this->Encoded) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $t;

return $this->Decoded;

}

function MakePattern($len = 10)
{
//possible letters
// E - Base64 Encode
// R - Reverse String
// I - Inverse Case
$poss = array('E','R', 'I');

//generate a string
for ( $i = 0 ; $i < $len ; $i++ )
{
$tmp[] = $poss[ rand(0,2) ];
}

//echo $str. "<br>";
//fix useless pattern section RR II
$str = implode(":", $tmp);

//fix
$str = str_replace( 'R:R:R:R:R:R' , 'R:E:R:E:R:E' , $str );
$str = str_replace( 'R:R:R:R:R' , 'R:E:R:E:R' , $str );
$str = str_replace( 'R:R:R:R' , 'R:E:R:E' , $str );
$str = str_replace( 'R:R:R' , 'R:E:R' , $str );
$str = str_replace( 'R:R' , 'R:E' , $str );

//fix
$str = str_replace( 'I:I:I:I:I:I' , 'I:E:I:E:I:E' , $str );
$str = str_replace( 'I:I:I:I:I' , 'I:E:I:E:I' , $str );
$str = str_replace( 'I:I:I:I' , 'I:E:I:E' , $str );
$str = str_replace( 'I:I:I' , 'I:E:I' , $str );
$str = str_replace( 'I:I' , 'I:E' , $str );

//string is good, set as pattern
$this->Pattern = $str;
return $this->Pattern; //if we need it

}

function PatternFlip($pattern)
{
//reverse the pattern
$str = strrev($pattern);

$ar = explode(":", $str);

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$tmp[] = "D";
break;
case "D":
$tmp[] = "E";
break;
case "R":
$tmp[] = "R";
break;
case "I":
$tmp[] = "I";
break;
}

}

$rev = implode(":", $tmp);

$this->PatternFlip = $rev;

return $this->PatternFlip;
}

// This is my custom Case Invertor!
// if you would like to use this in a script, please credit it to me, thank you
function InvertCase($str)
{
//Do initial conversion
$new = strtoupper( $str );

//spluit into arrays
$s = str_split( $str );
$n = str_split( $new );

//now we step through each letter, and if its the same as before, we swap it out
for ($i = 0; $i < count($s); $i++)
{
if ( $s[$i] === $n[$i] ) //SWAP THE LETTER
{
//ge the letter
$num = ord( $n[$i] );

//see if the ord is in the alpha ranges ( 65 - 90 | 97 - 122 )
if ( ( $num >= 65 AND $num <= 90 ) OR ( $num >= 97 AND $num <= 122 ) )
{
if ($num < 97 ) { $num = $num + 32; }
else { $num = $num - 32; }

$newchr = chr($num);

$n[$i] = $newchr;
}
}
}

//join the new string back together
$newstr = implode("", $n);

return $newstr;

}

}

?> ………… 从这个插件我需要使用编码和解码功能来实现我的功能......... 如果有人可以帮助我......这对我来说非常有用......

最佳答案

为什么不使用 json_encode?就这么做

$str=json_encode($array);

然后,发送数据,另一端做

$array=json_decode($str);

关于php - 为 ExtJS 树控件准备多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401471/

相关文章:

php - UPDATE 语句向其他行添加空白

javascript - 尽管 JS 函数更改了元素属性,但 IE8 v8 未更改 DOM 元素的类

javascript - Office ContentControls 列表不准确

javascript - vbox 中的 ExtJs DataView 高度

javascript - Extjs 网格宽度计算错误

php - 如何在 PHP 中将 Socks5 与 fsockopen 结合使用

php - 执行锁定文件(用于关键部分目的)清理的正确方法是什么

extjs - 如何使用 TreeStore 或 TreeEditor 组件更新数据?

php - 自动化构建和 STDIN

javascript - typescript :编译文件而不镜像目录层次结构