将JS代码转换为PHP

时间:2011-04-14 15:42:34

标签: php

var base64 = new Encodeutil.Base64Binary("");
    lstApplicableSubs = new Array(lstCategories.length);
    for (var i = 0; i < lstApplicableSubs.length; i++) lstApplicableSubs[i] = new Array();
    for (i = 0; i < lstSubCategories.length; i++)
    {
        var map = base64.decode(lstSubCategories[i].validFor);
        for (var j = 0; j < lstCategories.length; j++)
        {
            var bits = map.charCodeAt(j >> 3);
            if ((bits & (0x80 >> (j & 0x07))) != 0) lstApplicableSubs[j].push(lstSubCategories[i]);
        }
    }

任何人都可以帮忙将其转换为PHP吗?

下面是对象的var_export:

stdClass::__set_state(array(
  array (
    0 => 
    stdClass::__set_state(array(
       'label' => 'Category',
       'name' => 'Category',
       'optionslist' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'label' => 'Category1',
           'value' => 'Category1',
        )),
        1 => 
        stdClass::__set_state(array(
           'label' => 'Category2',
           'value' => 'Category2',
        )),
        2 => 
        stdClass::__set_state(array(
           'label' => 'Category3',
           'value' => 'Category3',
        )),
        3 => 
        stdClass::__set_state(array(
           'label' => 'Category4',
           'value' => 'Category4',
        )),
    )),
    1 => 
    stdClass::__set_state(array(
       'label' => 'Sub Category',
       'name' => 'Sub_Category',
       'optionslist' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'label' => 'SubtCategory1',
           'validFor' => '' . "\0" . '' . "\0" . '' . "\0" . '' . "\0" . '' . "\0" . '',
           'value' => 'SubtCategory1',
        )),
        1 => 
        stdClass::__set_state(array(
           'label' => 'SubtCategory2',
           'validFor' => '' . "\0" . '' . "\0" . '',
           'value' => 'SubtCategory2',
        )),
    ),
))

1 个答案:

答案 0 :(得分:1)

没有任何东西可以测试,甚至不知道目的,这是我最好的猜测:

// assuming $lstCategories & $lstSubCategories exist already...

$subs = count($lstSubCategories);
$lstApplicableSubs = array_fill(0,$subs,array());
for ($i = 0; $i < $subs; $i++)
{
  $map = base64_decode($lstSubCategories[$i]['validFor']);
  $cats = count($lstCategories);
  for ($j = 0; $j < $cats; $j++)
  {
    $bits = ord($map{$j >> 3});
    if (($bits & (0x08 >> ($j & 0x07))) != 0)
      $lstApplicableSubs[$j][] = $lstSubCategories[$i];
  }
}

此外,这假设lstSubCategories是一个键控数组。如果是对象,请将$lstSubCategories[$i]['validFor']更改为$lstSubCategories[$i]->validFor

您可以从中学习一些文档:

  • Working with Bits
  • base64_decode(与创建转换器相同,然后从中调用.decode
  • 由于charCodeAt实际上会返回unicode序号,因此您可能需要查看unicode conversion而不是我使用的ord
  • {}上的$map是对该字符串中字符索引的引用。例如$foo = 'Hi'; echo $foo{0} // returns H