Creative
Creative

Reputation: 1

Convert JS code to 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]);
        }
    }

Can anyone help convert this to PHP?

Below is var_export for object :

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',
        )),
    ),
))

Upvotes: 0

Views: 303

Answers (1)

Brad Christie
Brad Christie

Reputation: 101604

Without anything to test, or even know the purpose, here's my best-guess:

// 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];
  }
}

Also, this assumes lstSubCategories is a keyed array. If it's an object, change $lstSubCategories[$i]['validFor'] to something like $lstSubCategories[$i]->validFor

Some documentation for you to learn from:

  • Working with Bits
  • base64_decode (the same as creating a converter then called .decode from it)
  • Since charCodeAt actually returns a unicode ordinal, you may want to look in to a unicode conversion instead of the ord I used.
  • The {} on the $map is a reference to the character index within that string. e.g. $foo = 'Hi'; echo $foo{0} // returns H

Upvotes: 1

Related Questions