简体   繁体   English

将 JavaScript function 转换为 PHP

[英]Convert JavaScript function to PHP

I have a JS function, with array inputs.我有一个带有数组输入的 JS function。

For example:例如:

x=[ 239709880, 250229420, 109667654, 196414465, 13098 ] x=[239709880、250229420、109667654、196414465、13098]
y=[ 78135241, 54642792, 249 ] y=[78135241、54642792、249]

OR:或者:

x=[ 0, 0, 0, 0, 0, 0, 1 ] x=[ 0, 0, 0, 0, 0, 0, 1 ]
y=[ 78135241, 54642792, 249 ] y=[78135241、54642792、249]

OR:或者:

x=[ 49 ] x=[49]
y=[ 33 ] y=[33]

function bdiv(x,y) {
    var n=x.length-1, t=y.length-1, nmt=n-t, arr = []
    if(n < t || n==t && (x[n]<y[n] || n>0 && x[n]==y[n] && x[n-1]<y[n-1])) {
        arr['q']=[0]
        arr['mod']=x
        return arr
    }
    if(n==t && toppart(x,t,2)/toppart(y,t,2) <4) {
        var q=0, xx
        for(;;) {
            xx=bsub(x,y)
            if(xx.length==0) break
            x=xx; q++
        }
        arr['q']=[q]
        arr['mod']=x
        return arr
    }
    var shift, shift2
    shift2=Math.floor(Math.log(y[t])/log2)+1
    shift=bs-shift2

    if(shift) {
        x=x.concat()
        y=y.concat()
        for(i=t; i>0; i--) y[i]=((y[i]<<shift) & bm) | (y[i-1] >> shift2); y[0]=(y[0]<<shift) & bm
        if(x[n] & ((bm <<shift2) & bm)) { x[++n]=0; nmt++; }
        for(i=n; i>0; i--) x[i]=((x[i]<<shift) & bm) | (x[i-1] >> shift2); x[0]=(x[0]<<shift) & bm
    }
    var i, j, x2, y2,q=zeros(nmt+1)

    y2=zeros(nmt).concat(y)

    for(;;) {
        x2=bsub(x,y2)
        if(x2.length==0) break
        q[nmt]++
        x=x2
    }
    var yt=y[t], top=toppart(y,t,2)
    for(i=n; i>t; i--) {
        m=i-t-1
        if(i >= x.length)
            q[m]=1
        else if(x[i] == yt)
            q[m]=bm
        else
            q[m]=Math.floor(toppart(x,i,2)/yt)
        topx=toppart(x,i,3)
        while(q[m] * top > topx)
            q[m]--
        y2=y2.slice(1)
        x2=bsub(x,bmul([q[m]],y2))
        if(x2.length==0) {
            q[m]--
            x2=bsub(x,bmul([q[m]],y2))
        }
        x=x2
    }
    if(shift){
        for(i=0; i<x.length-1; i++)
            x[i]=(x[i]>>shift) | ((x[i+1] << shift2) & bm);
        x[x.length-1]>>=shift
    }
    while(q.length > 1 && q[q.length-1]==0)
        q=q.slice(0,q.length-1)
    while(x.length > 1 && x[x.length-1]==0)
        x=x.slice(0,x.length-1)
    arr['q']=q
    arr['mod']=x
    return arr;
}


What I have done under 5 days so far in PHP:到目前为止,我在 PHP 的 5 天内做了什么:

function bdiv($x,$y){
    global $bs, $bm, $bx2, $bx, $bd, $bdm, $log2;
    $arr=[];
    $n=count($x)-1;
    $t=count($y)-1;
    $nmt=$n-$t;

    if($n < $t || $n==$t && ($x[$n]<$y[$n] || $n>0 && $x[$n]==$y[$n] && $x[$n-1]<$y[$n-1]))
        return ['q'=>[0], 'mod'=>$x];

    if($n==$t && toppart($x,$t,2)/toppart($y,$t,2) <4){
        $q=0;
        for(;;){
            $xx=bsub($x,$y);
            if(count($xx)==0)
                break;
            $x=$xx;
            $q++;
        }
        return ['q'=>[$q], 'mod'=>$x];
    }

    $shift2=floor(log($y[$t])/$log2)+1;
    $shift=$bs-$shift2;
    if($shift){

/////////////////////////////////////////////////// the problem start here with the concat()
        array_push($x,$x);
        array_push($y,$y);

        for($i=$t; $i>0; $i--)
            $y[$i]=(($y[$i] << $shift) & $bm) | ($y[$i-1] >> $shift2);
        $y[0]=($y[0] << $shift) & $bm;
        if($x[$n] & (($bm << $shift2) & $bm)){
            $x[++$n]=0;
            $nmt++;
        }
        for($i=$n; $i > 0; $i--)
            $x[$i]=(($x[$i] << $shift) & $bm) | ($x[$i-1] >> $shift2);
        $x[0]=($x[0] << $shift) & $bm;
    }
    $q=zeros($nmt+1);

    array_push($arr, zeros($nmt));
    array_push($arr, $y);
    $y2=array_merge(...$arr);

    for(;;){
        $x2=bsub($x,$y2);
        if(count($x2)==0)
            break;
        $q[$nmt]++;
        $x=$x2;
    }

    $yt=$y[$t];
    $top=toppart($y,$t,2);

    for($i=$n; $i>$t; $i--){
        $m=$i-$t-1;
        if($i >= count($x))
            $q[$m]=1;
        else if($x[$i] == $yt)
            $q[$m]=$bm;
        else
            $q[$m]=floor(toppart($x,$i,2)/$yt);

        $topx=toppart($x,$i,3);
        while($q[$m] * $top > $topx)
            $q[$m]--;

        $y2=array_slice($y2,1);
        $x2=bsub($x,bmul([$q[$m]],$y2));

        if(count($x2)==0){
            $q[$m]--;
            $x2=bsub($x,bmul([$q[$m]],$y2));
        }
        $x=$x2;
    }

    if($shift){
        for($i=0; $i<count($x)-1; $i++)
            $x[$i]=($x[$i] >> $shift) | (($x[$i+1] << $shift2) & $bm);
        $x[count($x)-1] >>= $shift;
    }

    while(count($q) > 1 && $q[count($q)-1]==0)
        $q=array_slice($q, 0, count($q)-1);
    while(count($x) > 1 && $x[count($x)-1]==0)
        $x=array_slice($x, 0, count($x)-1);

    return ['q'=>$q, 'mod'=>$x];
}

So as I marked in the PHP code I have a problem with the array_push($x,$x) , seems like this is not the equivalent of x=x.concat() .因此,正如我在 PHP 代码中标记的那样,我对array_push($x,$x)有问题,似乎这不等同于x=x.concat() Array_push add the whole current $x values as a new element to the existing $x array: Array_push 将整个当前 $x 值作为新元素添加到现有$x数组中:

$x=[ 1, 2, 3 ]; $x=[ 1, 2, 3 ];
array_push($x,$x); array_push($x,$x);
then $x will be [ 1, 2, 3, [ 1, 2, 3 ] ]那么 $x 将是 [ 1, 2, 3, [ 1, 2, 3 ] ]

If I try to flatten the array ( $x=array_merge(...$x); ) then a new PHP error shows up: array_merge(): Argument #1 is not an array如果我尝试展平数组( $x=array_merge(...$x); ),则会出现一个新的 PHP 错误: array_merge(): Argument #1 is not an array

I would really appreciate it if anyone have any idea, how to convert properly this JS function to a PHP version.如果有人知道如何将此 JS function 正确转换为 PHP 版本,我将不胜感激。 Thanks in advance.提前致谢。

replace array_push with array_merge.用 array_merge 替换 array_push。

This will return the merged array then store the result in $x;这将返回合并后的数组,然后将结果存储在 $x;

array_merge is meant for arrays. array_merge 适用于 arrays。 it will take the values from one array and append it to the other.它将从一个数组中获取值,并将 append 获取到另一个数组。 just like concat does in JS.就像 JS 中的 concat 一样。

... splits the array ($x) into several values, this is not the correct input. ... 将数组 ($x) 拆分为多个值,这不是正确的输入。 it is the equivalent of array_merge(1,2,3) (ie arrays are not being inputted)它相当于 array_merge(1,2,3) (即没有输入 arrays)

$x = array(1,2,3);
$x = array_merge($x,$x);
var_dump($x); //output: [1,2,3,1,2,3]

echo $x[4]; // output: 2

if i've misunderstood the question please let me know.如果我误解了这个问题,请告诉我。

Use array_merge instead of array_push ie使用 array_merge 而不是 array_push 即

  $x = array(1, 2, 3);
       print_r(array_merge($x,$x));

I am confused why your JavaScript code even has:我很困惑为什么你的 JavaScript 代码甚至有:

x=x.concat()
y=y.concat()

They don't serve any purpose other than assigning a new copy of same array to itself.除了将同一数组的新副本分配给自身之外,它们没有任何用途。 If it was intended to avoid modification on the original array, then you can simply replace those two lines of code with:如果它是为了避免修改原始数组,那么您可以简单地将这两行代码替换为:

$x = array_merge(array(), $x);
$y = array_merge(array(), $y);

They both serve exact same purpose.它们都具有完全相同的目的。

At this point, I am not sure about rest of the php code that you wrote so far, but if it helps you in anyway, that is fine.在这一点上,我不确定您到目前为止编写的 php 代码的 rest,但如果它对您有帮助,那很好。

If what you are doing with x = x.concat() is attempting to ensure that the original passed array is not being modified by your function, you do not have to do anything to ensure that in your PHP version of this function because by default arguments will be copied rather than passed by reference .如果您对x = x.concat()所做的操作是试图确保您的 function 未修改原始传递的数组,则您无需执行任何操作来确保在您的 PHP 版本中此 ZC1C425268E6873845D1 默认为 AB arguments 将被复制而不是通过引用传递 To force an array to be passed by reference in PHP, you must preceded the argument name with an ampersand ( & ).要强制在 PHP 中通过引用传递数组,您必须在参数名称前加上 & 符号 ( & )。 This can be demonstrated with the following program where we have defined two functions that each modify the first element of the passed array.这可以通过以下程序进行演示,其中我们定义了两个函数,每个函数都修改传递数组的第一个元素。 In the first function, test1 , the array argument is copied so the original passed array remains unmodified.在第一个 function, test1中,复制了数组参数,因此原始传递的数组保持不变。 But in the second function, test2 , the array argument is passed by reference and when the function returns, the original passed array will have been modified.但是在第二个 function, test2中,数组参数是通过引用传递的,当 function 返回时,原始传递的数组将被修改。 The only difference between the two functions is that in test1 the arguments is defined as $x and in test2 as &$x :这两个函数之间的唯一区别是在test1中 arguments 被定义为$x而在test2中为&$x

<?php

// $x is passed by value:
function test1($x)
{
    $x[0] = 9;
    print_r($x);
}

// $x is passed by reference:
function test2(&$x)
{
    $x[0] = 9;
    print_r($x);
}

$my_array = [0, 1, 2];
echo "Pass by value:\n";
test1($my_array); //$my_array remains unmodified
print_r($my_array);

echo "\n\nPass by reference:\n";
test2($my_array); //$my_array is modified
print_r($my_array);

Prints:印刷:

Pass by value:
Array
(
    [0] => 9
    [1] => 1
    [2] => 2
)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
)


Pass by reference:
Array
(
    [0] => 9
    [1] => 1
    [2] => 2
)
Array
(
    [0] => 9
    [1] => 1
    [2] => 2
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM