【问题标题】:Preparing a multi-dimensional array for an ExtJS tree control为 ExtJS 树控件准备多维数组
【发布时间】:2011-03-23 06:11:57
【问题描述】:

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

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
)

);

所以像这样我只需要编码字符串,带分隔符的变量............ 后来所有的编码值在它被带回后端之前被解码.....为此我必须使用这个插件............ encode.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;

}

}

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

【问题讨论】:

  • 外部参考:Numerouspreviousquestions感谢您这次真正告诉我们您要完成什么
  • 现在,@0001,你能告诉我们为什么你“必须”使用这种可怕的、倒退的、愚蠢的编码方案吗?如果您正在使用 ExtJS,那么为什么不使用 JSON, 是与 Javascript 交换数据的第一最佳格式?
  • 我已经更新了这个问题的标题和标签,希望能引起一些有用的关注。
  • 我被严格要求只使用那个可怕的插件............:-( ....ya 在编码数据后我需要将 json 格式发送到前端的编码数据......
  • 我很犹豫要问,但是 ExtJS 树控件中有代码来处理这种编码吗?如果不是,他们如何期望您在数据存在后实际使用这些数据?您要么需要在 Javascript 中重新实现解码器,要么为了解码而对 PHP 进行 another 调用,这真的很愚蠢

标签: php javascript extjs


【解决方案1】:

为什么不使用 json_encode?做吧

$str=json_encode($array);

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

$array=json_decode($str);

【讨论】:

    【解决方案2】:

    好的,让我们分解问题。

    你有一个数组。数组中的每个元素都是一个哈希。该散列中的一个(或多个)值必须使用库中可怕的可憎性进行编码。但是库不能处理数组。

    我们必须自己处理数组。

    &lt;rant&gt;
    在我们开始之前,我想再次表达一下“保护者”代码的设计是多么可怕。它是为 PHP4 编写的,实际上是封装在一个类中的意大利面条代码。它误用了属性,就好像用户对 Java 的 实例变量的工作方式有某种错误记忆,并以某种方式认为以同样的方式使用 PHP 是合适的或理智的。如果代码的作者现在不以完全不屑的态度回顾那段令人反感的字节,那他就大错特错了。
    &lt;/rant&gt;

    我将以this copy of it 为基础了解这门课程,因为您提供的课程的格式比原来的还要糟糕。

    首先,让我们创建一个需要编码的内部数组键列表。

    $keys_to_encode = array( 'user' );
    

    您的示例编码仅将 user 键列为可编码。如果您需要对其他人进行编码,只需向该数组添加更多元素即可。

    现在,让我们准备我们的“保护者”。似乎希望您指定一个模式,或者使用MakePattern 方法让它创建一个。 我们将手动指定一个,因为MakePattern 可以提出有效的无用组合。

    $stupid = new Protector();
    $stupid->Pattern = 'E:I:E:R:D:I:E';
    

    这将进行base64编码,翻转大小写,再次base64,反转它,取消base64,翻转大小写,然后重新base64。请注意,PHP 的 base64 解码器“正确”忽略了错误的填充和意外字符,这是 base64-reverse-unbase64 工作的唯一原因。对于不知道 base64 是什么样子的人来说,生成的字符串看起来像乱码,而对于知道 base64 是什么样子的人来说,un-base64 会乱码。

    如果您需要以某种方式对值进行编码,您只需更改模式即可。对模式进行硬编码或将其与数据一起存储在某处很重要,因为没有它,您将很难进行解码。 (我的意思是,它可以手工完成,但你不想这样做。)我希望你的老板会给你关于模式的具体说明,因为你没有选择在课堂上使用这种彻底的失败。

    现在是时候处理我们的数据了。让我们假设 $in 包含您的原始数组。

    $out = array();
    foreach($in as $k => $target) {
        foreach($keys_to_encode as $target_key) {
            $stupid->ToEncode = $target[ $target_key ];
            $target[ $target_key ] = $stupid->Encode();
        }
        $out[$k] = $target;
    }
    

    这会遍历散列数组,然后在每个散列中,只有我们要编码的键被编码。编码后的散列被放置在一个新数组$out 中。这就是您要传递给树小部件的内容。

    解码同样简单。您已经说过您的目标是让用户编辑树小部件中的某些数据,并且您似乎想要保护底层密钥。这告诉我您可能一次只需要处理一个哈希值。因此,我将仅使用一个值 $whatever 来编写此解码示例。

    $stupidest = new Protector();
    $stupidest->Pattern = 'E:I:E:R:D:I:E'; // SAME PATTERN!
    $stupidest->ToDecode = $whatever;
    $decoded = $stupidest->Decode();
    

    同样,关键您在此处使用相同的解码模式。

    至于与树小部件的实际交互,您需要自己处理。我只知道 ExtJS 的存在,并且知道它的 GUI 创建者真的很有趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多