Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中合并一些数组元素?_Php_Arrays - Fatal编程技术网

如何在php中合并一些数组元素?

如何在php中合并一些数组元素?,php,arrays,Php,Arrays,例如: Array ( [0] => 35 [1] => - [2] => 59 [3] => * [4] => 2 [5] => / [6] => 27 [7] => * [8] => 2 ) 然后计算: 59*2=118 新阵列是: Array ( [0] => 35 [1] => - [2] =>

例如:

Array (
     [0] => 35
     [1] => -
     [2] => 59
     [3] => *
     [4] => 2
     [5] => /
     [6] => 27
     [7] => *
     [8] => 2 )
然后计算:

59*2=118

新阵列是:

 Array (
    [0] => 35
     [1] => -
     [2] => 118
     [3] => /
     [4] => 27
     [5] => *
     [6] => 2 )
这是我的原始资料来源:
input($\u POST['numbers'])
是一个字符串,如:

65*6/6+5-5

class calculator {
    //property
    private $str='';
    private $len=0;
    private $ar_str=array();
    private $ar_design=array();
    private $ar_sum=array();
    private $ar_min=array();
    private $ar_mult=array();
    private $ar_divi=array();

//Method
public function __construct($str1=''){
    $this->str=$str1;
    $this->len=strlen($this->str);
    $this->ar_str=str_split($this->str);
    if($this->ar_str[0] == '+' ||
    $this->ar_str[0] == '-' ||
    $this->ar_str[0] == '*' ||
    $this->ar_str[0] == '/' ||
    $this->ar_str[$this->len-1] == '+' ||
    $this->ar_str[$this->len-1] == '-' ||
    $this->ar_str[$this->len-1] == '*' ||
    $this->ar_str[$this->len-1] == '/'
    ){
        exit("Syntax error!");
    }else if(!filter_var($this->ar_str[0], FILTER_VALIDATE_INT)){
        exit("just use numbers and 4 operators!");
    }
    $this->ar_design[0]=$this->ar_str[0];
    //start for
    $j=1;
    for($i=1;$i<$this->len;$i++){
        if($this->ar_str[$i] == '+' || $this->ar_str[$i] == '-' || $this->ar_str[$i] == '*' || $this->ar_str[$i] == '/'){
            if($this->ar_str[$i-1] == '+' || $this->ar_str[$i-1] == '-' || $this->ar_str[$i-1] == '*' || $this->ar_str[$i-1] == '/'){
                exit("Syntax error!");
            }else{
                $this->ar_design[$j]=$this->ar_str[$i];
                $j++;
            }
        }else if(filter_var($this->ar_str[$i], FILTER_VALIDATE_INT)){
            if($this->ar_str[$i-1] == '+' || $this->ar_str[$i-1] == '-' || $this->ar_str[$i-1] == '*' || $this->ar_str[$i-1] == '/'){
                $this->ar_design[$j]=$this->ar_str[$i];
            }else{
                $j--;
                $this->ar_design[$j]=$this->ar_design[$j].$this->ar_str[$i];
            }
        $j++;
        }else{
            exit("just use numbers and 4 operators!");
        }
    }//end of for

    print_r($this->ar_design);//array this array should be calculate!!!!!
}//end construct
我可以找到答案:

$this->len_d=count($this->ar_design);
        $this->ar_cal[0]=$this->ar_design[0];
        $k=1;
        for($i=1;$i<$this->len_d;$i++){
            if($this->ar_design[$i] == '*'){
                $k--;
                $this->ar_cal[$k]=$this->ar_design[$i-1]*$this->ar_design[$i+1];
                $i++;
            }else{
                $this->ar_cal[$k]=$this->ar_design[$i];
            }
            $k++;
        }
        print_r($this->ar_cal);
$this->len\u d=count($this->ar\u设计);
$this->ar_-cal[0]=$this->ar_-design[0];
$k=1;
对于($i=1;$ilen\u d;$i++){
如果($this->ar_design[$i]='*'){
$k--;
$this->ar_-cal[$k]=$this->ar_-design[$i-1]*$this->ar_-design[$i+1];
$i++;
}否则{
$this->ar_cal[$k]=$this->ar_design[$i];
}
$k++;
}
打印($this->ar\u cal);

您可以使用一个循环来完成它,然后执行它

   $array=Array (35,'-',59,'*',2,'/',27,'*',2);
        foreach ($array as $value){
          $stringify.=$value;
        }
        echo 'Calculation looks like this: '.$stringify.'<br/>';

        function calculate( $math ){
            $calc = create_function("", "return (" .$math. ");" );
            return $calc();
        }   
        echo calculate($stringify);
$array=array(35'-',59'*',2'/',27'*',2);
foreach($array作为$value){
$stringify.=$value;
}
echo'计算结果如下:'.$stringify.
'; 函数计算($math){ $calc=create_函数(“,”返回(“.$math”);”; 返回$calc(); } 回波计算($stringify);
可以改进,例如通过验证输入


工作示例:

您可以使用循环执行它

   $array=Array (35,'-',59,'*',2,'/',27,'*',2);
        foreach ($array as $value){
          $stringify.=$value;
        }
        echo 'Calculation looks like this: '.$stringify.'<br/>';

        function calculate( $math ){
            $calc = create_function("", "return (" .$math. ");" );
            return $calc();
        }   
        echo calculate($stringify);
$input = "35-59*2/27*2";

if (preg_match('/[^-+*\/\\d]/', $input)) // simple checking
{
    echo "just use numbers and 4 operators!"; 
}
else
{
    $substraction = explode('-', $input);
    foreach ($substraction as $pos_s => $sub)
    {
        $addition = explode('+', $sub);
        foreach ($addition as $pos_a => $add)
        {
            $multiplication = explode('*', $add);
            foreach ($multiplication as $pos_m => $mult)
            {
                $division = explode('/', $mult);
                $d = $division[0];
                for ($i=1; $i < count($division);$i++)              
                    $d = $d / $division[$i];

                $multiplication[$pos_m] = $d;
            }
            $m = $multiplication[0];
            for ($i=1; $i < count($multiplication);$i++)                
                $m = $m * $multiplication[$i];

            $addition[$pos_a] = $m;
        }
        $a = $addition[0];
        for ($i=1; $i < count($addition);$i++)              
            $a = $a + $addition[$i];

        $substraction[$pos_s] = $a;
    }
    $result = $substraction[0];
    for ($i=1; $i < count($substraction);$i++)              
        $result = $result - $substraction[$i];
    echo $input . " = " . $result; 
}
$array=array(35'-',59'*',2'/',27'*',2);
foreach($array作为$value){
$stringify.=$value;
}
echo'计算结果如下:'.$stringify.
'; 函数计算($math){ $calc=create_函数(“,”返回(“.$math”);”; 返回$calc(); } 回波计算($stringify);
可以改进,例如通过验证输入


工作示例:

为什么不一边计算一边将输入拆分成一个数组,然后只输出结果呢?
$input = "35-59*2/27*2";

if (preg_match('/[^-+*\/\\d]/', $input)) // simple checking
{
    echo "just use numbers and 4 operators!"; 
}
else
{
    $substraction = explode('-', $input);
    foreach ($substraction as $pos_s => $sub)
    {
        $addition = explode('+', $sub);
        foreach ($addition as $pos_a => $add)
        {
            $multiplication = explode('*', $add);
            foreach ($multiplication as $pos_m => $mult)
            {
                $division = explode('/', $mult);
                $d = $division[0];
                for ($i=1; $i < count($division);$i++)              
                    $d = $d / $division[$i];

                $multiplication[$pos_m] = $d;
            }
            $m = $multiplication[0];
            for ($i=1; $i < count($multiplication);$i++)                
                $m = $m * $multiplication[$i];

            $addition[$pos_a] = $m;
        }
        $a = $addition[0];
        for ($i=1; $i < count($addition);$i++)              
            $a = $a + $addition[$i];

        $substraction[$pos_s] = $a;
    }
    $result = $substraction[0];
    for ($i=1; $i < count($substraction);$i++)              
        $result = $result - $substraction[$i];
    echo $input . " = " . $result; 
}
举个例子:

$input = "35-59*2/27*2";

if (preg_match('/[^-+*\/\\d]/', $input)) // simple checking
{
    echo "just use numbers and 4 operators!"; 
}
else
{
    $substraction = explode('-', $input);
    foreach ($substraction as $pos_s => $sub)
    {
        $addition = explode('+', $sub);
        foreach ($addition as $pos_a => $add)
        {
            $multiplication = explode('*', $add);
            foreach ($multiplication as $pos_m => $mult)
            {
                $division = explode('/', $mult);
                $d = $division[0];
                for ($i=1; $i < count($division);$i++)              
                    $d = $d / $division[$i];

                $multiplication[$pos_m] = $d;
            }
            $m = $multiplication[0];
            for ($i=1; $i < count($multiplication);$i++)                
                $m = $m * $multiplication[$i];

            $addition[$pos_a] = $m;
        }
        $a = $addition[0];
        for ($i=1; $i < count($addition);$i++)              
            $a = $a + $addition[$i];

        $substraction[$pos_s] = $a;
    }
    $result = $substraction[0];
    for ($i=1; $i < count($substraction);$i++)              
        $result = $result - $substraction[$i];
    echo $input . " = " . $result; 
}
$input=“35-59*2/27*2”;
if(preg_match('/[^-+*\/\\d]/',$input))//简单检查
{
echo“只需使用数字和4个运算符!”;
}
其他的
{
$substraction=分解('-',$input);
foreach($pos\u s=>$sub的减法)
{
$addition=分解(“+”,$sub);
foreach($pos_a=>$add)
{
$乘法=分解('*',$add);
foreach($pos_m=>$mult的乘法)
{
$division=分解('/',$mult);
$d=$division[0];
对于($i=1;$i
印刷品

35-59*2/27*2=26.259259259259


为什么不一边计算一边将输入拆分成一个数组,然后输出结果呢? 举个例子:

$input = "35-59*2/27*2";

if (preg_match('/[^-+*\/\\d]/', $input)) // simple checking
{
    echo "just use numbers and 4 operators!"; 
}
else
{
    $substraction = explode('-', $input);
    foreach ($substraction as $pos_s => $sub)
    {
        $addition = explode('+', $sub);
        foreach ($addition as $pos_a => $add)
        {
            $multiplication = explode('*', $add);
            foreach ($multiplication as $pos_m => $mult)
            {
                $division = explode('/', $mult);
                $d = $division[0];
                for ($i=1; $i < count($division);$i++)              
                    $d = $d / $division[$i];

                $multiplication[$pos_m] = $d;
            }
            $m = $multiplication[0];
            for ($i=1; $i < count($multiplication);$i++)                
                $m = $m * $multiplication[$i];

            $addition[$pos_a] = $m;
        }
        $a = $addition[0];
        for ($i=1; $i < count($addition);$i++)              
            $a = $a + $addition[$i];

        $substraction[$pos_s] = $a;
    }
    $result = $substraction[0];
    for ($i=1; $i < count($substraction);$i++)              
        $result = $result - $substraction[$i];
    echo $input . " = " . $result; 
}
$input=“35-59*2/27*2”;
if(preg_match('/[^-+*\/\\d]/',$input))//简单检查
{
echo“只需使用数字和4个运算符!”;
}
其他的
{
$substraction=分解('-',$input);
foreach($pos\u s=>$sub的减法)
{
$addition=分解(“+”,$sub);
foreach($pos_a=>$add)
{
$乘法=分解('*',$add);
foreach($pos_m=>$mult的乘法)
{
$division=分解('/',$mult);
$d=$division[0];
对于($i=1;$i
印刷品

35-59*2/27*2=26.259259259259



你到底在问什么。更具体一点?我尝试计算这个数组并打印答案!这是一个知道优先级的计算器。我不画计算!我用PHP做这些…考虑实现一个简单的。您只需要一个堆栈并引入操作顺序。59*2=118 ok。那么118/27=呢?等等……你到底在问什么。更具体一点?我尝试计算这个数组并打印答案!这是一个知道优先级的计算器。我不画计算!我用PHP做这些…考虑实现一个简单的。您只需要一个堆栈并引入操作顺序。59*2=118 ok。那么118/27=呢?诸如此类..?你可以用PHP来做这件事,而且永远不用
eval()
@nickb请告诉我,怎么做?这就是我提出的解决方案。很简单,你有一个定义的数字运算符列表,并使用内置的PHP运算符通过解析输入表达式来计算任何操作的结果。你是对的,考虑到操作符在计算35-118/27*2时的优先级,答案是32.814814814815,而不是26.259259259259!!!你可以用PHP来实现这一点,但永远不要使用
eval()
@nickb。请告诉我,怎么做?这就是我想出的解决办法。很简单,你有