Bruno Andrade
Bruno Andrade

Reputation: 595

Looking for the equivalent of this JS function in PHP

How can I get the same result with this JavaScript function with PHP 5.4 ?

  var item = ["footer", "__cpsExtensionHeader", "__cpsUrl", "__cpsPermalinkContainer", "setProperty", "translateY(-", "px)", "important", "style", "data---cpt", "fixed", "position", "auto", "transform", "translateY(", "transition"];
  (function(params, count) {
    var fn = function(selected_image) {
      for (; --selected_image;) {
        params["push"](params["shift"]());
      }
    };
    fn(++count);
  })(item, 224);
  var $ = function(i, fn) {

    i = i - 0;
    var id = item[i];
    return id;
  };

I've tried this:

$arrays = ["footer", "__cpsExtensionHeader", "__cpsUrl", "__cpsPermalinkContainer", "setProperty", "translateY(-", "px)", "important", "style", "data---cpt", "fixed", "position", "auto", "transform", "translateY(", "transition"];

call_user_func_array(
    function ($params, $count) {
        $fn = function($selected_image) {
            for (; --$selected_image;) {
                $var = array_push(array_shift($params), $selected_image);
            }
        };
        $fn(++$count);
    },
    array(&$arrays, 240)
);

But I stopped and I can't pass the parameter correctly to the self-called function

array_shift() expects parameter 1 to be an Array, null given in

Upvotes: 0

Views: 115

Answers (1)

MaartenDev
MaartenDev

Reputation: 5811

The javascript version has a complicated construction because it creates a new so called function scope. This isn't needed in PHP and the script can thus be simplied to:

function iterate($params, $count){
    $closure = function ($selectedImage) use(&$counter, &$params){
        for (; --$selectedImage;) {
            array_push($params, array_shift($params));

            // For debug only
            echo implode(',',$params) . PHP_EOL;
      }
    };

    $closure(++$count);
}

iterate($items, 224);

result:

__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-,px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer
__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-,px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader
__cpsPermalinkContainer,setProperty,translateY(-,px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl
setProperty,translateY(-,px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer
translateY(-,px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty
px),important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-
important,style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-,px)
style,data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-,px),important
data---cpt,fixed,position,auto,transform,translateY(,transition,footer,__cpsExtensionHeader,__cpsUrl,__cpsPermalinkContainer,setProperty,translateY(-,px),important,style
etc...

Upvotes: 1

Related Questions