2013-08-28 67 views
0

我的目標是將所有數組值保存在另一個數組中,但我不知道該怎麼做。 這裏是我的簡單數組:如何從數組中創建多維數組並執行過濾?

$grand_total = $total + $cost; 
$cost_ratio = ($cost/$grand_total) * 100; 
$paid_ratio = ($total/$grand_total) * 100; 

$info[] = array(
      'id' => $data['id'], 
      'ratio' => $cost_ratio, 
      'status' => $status 
     ); 

這裏是我的樣品陣列輸出

Array 
(
[0] => Array 
    (
     [id] => 53 
     [ratio] => 100 
     [status] => C 
    ) 

[1] => Array 
    (
     [id] => 57 
     [ratio] => 100 
     [status] => I 
    ) 

[2] => Array 
    (
     [id] => 60 
     [ratio] => 1.3157894736842 
     [status] => I 
    ) 

創建一個數組的下一個要做的事情之後是獲得比過濾此。 正如你在上面看到的,我有一個變量$cost_ratio,這個變量是將過濾我的數組。

這裏是我的代碼的延續:用於顯示我的記錄

//declare a variable 
$var1 = 0; 
$var2 = 0; 
$var3 = 0; 
$var4 = 0; 
$var5 = 0; 
$var6 = 0; 
$var7 = 0; 
$var8 = 0; 
$var9 = 0; 
$var10 = 0; 

for($x = 0; $x < sizeof($info); $x++){ 

      $status_info = $info[$x]['status']; 
      $temp = $info[$x]['ratio']; 

      /*this will set my filter*/ 

      /*if temp <= 9, i need to put all of my array that has a ratio <= 9*/ 

      if($temp <= 9){ 
       $var1++; 
      }else if($temp <= 19){ 
       $var2++; 
      }else if($temp <= 29){ 
       $var3++; 
      }else if($temp <= 39){ 
       $var4++; 
      }else if($temp <= 49){ 
       $var5++; 
      }else if($temp <= 59){ 
       $var6++; 
      }else if($temp <= 69){ 
       $var7++; 
      }else if($temp <= 79){ 
       $var8++; 
      }else if($temp <= 89){ 
       $var9++; 
      }else{ 
       $var10++; 
      } 

     } 

後最後表

echo "<table border='1'>"; 

     echo "<tr>"; 
      echo "<td>PERCENTAGE RANGE</td>"; 
      echo "<td>VOLUME</td>"; 
      echo "<td>RATIO</td>"; 
      echo "<td>BACKORDERED</td>"; 
      echo "<td>CANCELED</td>"; 
      echo "<td>COD APPROVED</td>"; 
      echo "<td>COD PENDING</td>"; 
      echo "<td>COD SHIPPED</td>"; 
      echo "<td>COMPLETED</td>"; 
      echo "<td>DECLINED</td>"; 
      echo "<td>FAILED</td>"; 
      echo "<td>FRAUD CHECKING</td>"; 
      echo "<td>PAID & WAITING SHIPPING</td>"; 
      echo "<td>PENDING</td>"; 
      echo "<td>SHIPPED</td>"; 
      echo "<td>TEST</td>"; 
      echo "<td>WAITING FOR APPROVAL</td>"; 
      echo "<td>WAITING PAYMENT</td>"; 
     echo "</tr>"; 

     echo "<tr>"; 
      echo "<td>10% - 90%</td>"; 
      echo "<td>".$var1."</td>"; <!-- IF ratio has a value between 1 - 9 go here --> 
      echo "<td></td>"; 
      echo "<td>".$x1."</td>"; 
      echo "<td>".$x2."</td>"; 
      echo "<td>".$x3."</td>"; 
      echo "<td>".$x4."</td>"; 
      echo "<td>".$x5."</td>"; 
      echo "<td>".$x6."</td>"; 
      echo "<td>".$x7."</td>"; 
      echo "<td>".$x8."</td>"; 
      echo "<td>".$x9."</td>"; 
      echo "<td>".$x10."</td>"; 
      echo "<td>".$x11."</td>"; 
      echo "<td>".$x12."</td>"; 
      echo "<td>".$x13."</td>"; 
      echo "<td>".$x14."</td>"; 
      echo "<td>".$x15."</td>";   
     echo "</tr>"; 

     echo "<tr>"; 
      echo "<td>20% - 80%</td>"; <!-- IF ratio has a value between 10 - 19 go here --> 
      echo "<td>".$var2."</td>"; 
      echo "<td></td>"; 
      echo "<td>".$x1."</td>"; 
      echo "<td>".$x2."</td>"; 
      echo "<td>".$x3."</td>"; 
      echo "<td>".$x4."</td>"; 
      echo "<td>".$x5."</td>"; 
      echo "<td>".$x6."</td>"; 
      echo "<td>".$x7."</td>"; 
      echo "<td>".$x8."</td>"; 
      echo "<td>".$x9."</td>"; 
      echo "<td>".$x10."</td>"; 
      echo "<td>".$x11."</td>"; 
      echo "<td>".$x12."</td>"; 
      echo "<td>".$x13."</td>"; 
      echo "<td>".$x14."</td>"; 
      echo "<td>".$x15."</td>";   
     echo "</tr>"; 
. 
. 

這就是我想做的事:

[ratio between 1 - 9 value] = array(
    [0] = array(
       'id' => 1, 
       'status' => 'C' 
      ), 
    [1] = array(
       'id' => 3, 
       'status' => 'D' 
      ), 
    [2] = array(
       'id' => 6, 
       'status' => 'J' 
      ), 

), 

[ratio between 10 - 19 value] = array(
    [0] = array(
       'id' => 1, 
       'status' => 'C' 
      ), 
    [1] = array(
       'id' => 3, 
       'status' => 'D' 
      ), 
    [2] = array(
       'id' => 6, 
       'status' => 'J' 
      ), 

), 
. 
. 
. 

這就是我想要做的,但我很難與此。

+0

這是一個很長的問題,並且混淆了你想要做的事情。你想在特定範圍內將「比例」分組還是其他問題? – invisal

+0

是的,我想按比例組合 – Jerielle

回答

1

則可以通過執行像下面容易組陣列:

$array = array(
    array('id' => 53, 'ratio' => 100, 'status' => 'I'), 
    array('id' => 54, 'ratio' => 50, 'status' => 'C'), 
    array('id' => 56, 'ratio' => 42, 'status' => 'D') 
); 

// Grouping array by ratio 
$group = array(); 
foreach($array as $value) { 
    $groupIndex = min($value['ratio']/10, 9); 
    $group[$groupIndex][] = $value; 
} 

var_dump($group); 

下面是示例輸出

 
array (size=3) 
    9 => 
    array (size=1) 
     0 => 
     array (size=3) 
      'id' => int 53 
      'ratio' => int 100 
      'status' => string 'I' (length=1) 
    5 => 
    array (size=1) 
     0 => 
     array (size=3) 
      'id' => int 54 
      'ratio' => int 50 
      'status' => string 'C' (length=1) 
    4 => 
    array (size=1) 
     0 => 
     array (size=3) 
      'id' => int 56 
      'ratio' => int 42 
      'status' => string 'D' (length=1) 

注意的是,指數9從90+裝置,5是指從50至59 ,而4意味着從40到49.如果你對索引約定感到困惑,這裏是完整列表:

$group[0] <--- from 0 to 9 
$group[1] <--- from 10 to 19 
$group[2] <--- from 20 to 29 
$group[3] <--- from 30 to 39 
$group[4] <--- from 40 to 49 
$group[5] <--- from 50 to 59 
$group[6] <--- from 60 to 69 
$group[7] <--- from 70 to 79 
$group[8] <--- from 80 to 89 
$group[9] <--- from 90+ 
+0

好吧我會這樣做 – Jerielle

+0

按比例分組數組我是否需要更改/ 10,9的值? – Jerielle

+0

取決於你想要什麼,如果你想按10的組別比率,你應該堅持我的代碼。 – invisal