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

时间:2013-02-18 14:12:22

标签: 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] => 118
     [3] => /
     [4] => 27
     [5] => *
     [6] => 2 )

这是我原来的来源: input ($_POST['numbers'])是一个字符串,LIKE:

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

}

if(isset($_POST['numbers'])){
    $num_str=trim($_POST['numbers']);
    if($num_str!=''){
        $num_str = str_replace('`','+',$num_str);
        new calculator($num_str);
    }
}

我能找到答案:

$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);

3 个答案:

答案 0 :(得分:3)

你可以用循环然后执行它。

   $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);

可以被改进,例如通过验证输入......

工作示例:http://allanthya.net/arrcalc2.php

答案 1 :(得分:0)

<?php
    $array = array( 35, '-', 59, '*', 2, '/', 27, '*', 2 );
    print_r($array);
    echo "<p>";

    while ( true ) {
        $div = array_keys($array, '/');
        $mult = array_keys($array, '*');
        $add = array_keys($array, '+');
        $sub = array_keys($array, '-');

        if ( count($div) >= 1 ) {
            $index = $div[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 / $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($mult) >= 1 ) {
            $index = $mult[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 * $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($add) >= 1 ) {
            $index = $add[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 + $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($sub) >= 1 ) {
            $index = $sub[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 - $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        }
        else {
            break;
        }
    }
    print_r($array);
    echo "<p>";
?>

在while循环的每次迭代之后继承print_r()的输出,这样你就可以看到它逐个遍历除法,乘法,加法和减法,然后修改数组:

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

Array ( [0] => 35 [1] => - [2] => 59 [3] => * [4] => 0.0740740740741 [5] => * [6] => 2 )

Array ( [0] => 35 [1] => - [2] => 4.37037037037 [3] => * [4] => 2 )

Array ( [0] => 35 [1] => - [2] => 8.74074074074 )

Array ( [0] => 26.2592592593 ) 

答案 2 :(得分:0)

为什么不在将输入分成数组时进行计算,然后只输出结果? 仅作为一个例子:

$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; 
}

打印

  

35-59 * 2/27 * 2 = 26.259259259259