0
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',
        )),
    ),
))
6
  • Which part of it did you get stuck at? What line ist troubling you? Commented Apr 14, 2011 at 15:45
  • I would say learn PHP. There is no converter here all are programmer. Commented Apr 14, 2011 at 15:46
  • 1
    @Martin : i am stuck with var bits = map.charCodeAt(j >> 3); if ((bits & (0x80 >> (j & 0x07))) != 0) i have not experice of working with that kind of code. Commented Apr 14, 2011 at 15:50
  • @Shakti : Well boss i am a new programmer. I thought this community was to help. Well if u feel like feel free to point me to some good tutorial on working with byte shift in php. Thanks. Commented Apr 14, 2011 at 15:51
  • Then your question is misleading. I suppose if you understood the functionality of these code lines (which I don't, because I didn't look at it in detail), you would know how to write them in PHP, too. So you might want to rephrase your question to "Can someone please explain the code in lines ... to me?" or such. Commented Apr 14, 2011 at 15:54

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

14 Comments

i get notice on following line : $bits = ord($map{$j >> 3}); Notice: Uninitialized string offset: 0 in C:\xampplite\htdocs\phptoolkit\case.php on line 64
@Creative: Then $map isn't being populated. Again, I don't have anything to test on, just what I see in the original code block. Make sure $lstSubCategories is an array, and see how you need to access validFor.
@Creative: So, assuming that $lstSubCategories is assigned the array from [optionslist] => Array, you should use the $lstSubCategories[$i]->validFor alternative.
But my problem is after substituting alternative for array for code takes a long time to execute and outputs that "Notice" and doesnt do anything...
@creative: Can you use var_export on your array so I can have it to test against? I'm lazy and don't want to re-create the structure. ;p
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.