2012-11-15 61 views
0

排序我得到這個數組:陣列與條件

Array 
(
    [0] => Array 
     (
      [MONTH] => April 
      [logs-count] => 14 
      [log_type] => 0 
     ) 

    [1] => Array 
     (
      [MONTH] => August 
      [logs-count] => 942 
      [log_type] => 0 
     ) 

    [2] => Array 
     (
      [MONTH] => July 
      [logs-count] => 132 
      [log_type] => 0 
     ) 

    [3] => Array 
     (
      [MONTH] => June 
      [logs-count] => 114 
      [log_type] => 0 
     ) 

    [4] => Array 
     (
      [MONTH] => March 
      [logs-count] => 424 
      [log_type] => 0 
     ) 

    [5] => Array 
     (
      [MONTH] => May 
      [logs-count] => 79 
      [log_type] => 0 
     ) 

    [6] => Array 
     (
      [MONTH] => November 
      [logs-count] => 269 
      [log_type] => 0 
     ) 

    [7] => Array 
     (
      [MONTH] => October 
      [logs-count] => 4447 
      [log_type] => 0 
     ) 

    [8] => Array 
     (
      [MONTH] => October 
      [logs-count] => 3 
      [log_type] => 1 
     ) 


    [9] => Array 
     (
      [MONTH] => September 
      [logs-count] => 1003 
      [log_type] => 0 
     ) 

) 

我想把這個值2個陣列,其中:每個月if log_type = 0,去1個陣列和if log_type = 1,去2陣列,但是,如果有日誌從F(前十一月,他的log_type是0到1個陣列,但到第二次去0)

所以最終它應該是這樣的,例如上面:

1陣列

Array 
(
    [0] => 14 
    [1] => 942 
    [2] => 132 
    [3] => 114 
    [4] => 424 
    [5] => 79 
    [6] => 272 
    [7] => 4447 
    [8] => 1003 
) 

2陣列

Array 
(
    [0] => 0 
    [1] => 0 
    [2] => 0 
    [3] => 0 
    [4] => 0 
    [5] => 0 
    [6] => 0 
    [7] => 3 
    [8] => 0 
) 
+0

這真的很難理解你想要做什麼。你能有更多的細節嗎? – Jean

+0

我明白了這個問題,直到:「..但是如果有從f的日誌(去年11月和他的log_type是0去1陣列,但到第二去0)」 – billyonecan

+0

@比利同樣在這裏 – Jean

回答

0

你應該使用foreach循環和一些conditionnal陳述你的日誌條目進行排序:

$array1 = array(); 
$array2 = array(); 

foreach ($input_array as $entry) { 
    if ($entry['MONTH']=='March' && $entry['log_type']==1) { 
    $array1[] = $entry['logs-count']; 
    } else if // Etc. 
} 

根據自己的口味調整條件語句。

1
$arr0 = []; 
$arr1 = []; 

foreach ($arr as $a) { 

    switch ($a['log_type']) { 

     case 0: 
      $arr0[] = $a['logs-count']; 
      break; 
     case 1: 
      $arr1[] = $a['logs-count']; 
      break; 
    } 

} 

這將做你的問題的可理解的部分。

0

嘗試:

$arr = array(array('MONTH' => 'April', 'logs-count' => 14,'log_type' => 0), 
array(...), 
... 
    ); //your array 
$arr1 = $arr2 = array(); 
foreach($arr as $v) { 
if($v['log_type'] === 0) { 
    $arr1[] = $v['logs-count']; 
    $arr2[] = 0; 
} 
else if($v['log_type'] === 1) 
    $arr2[] = $v['logs-count']; 

}