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からのログがある場合(たとえば、11月で彼のlog_typeが0の場合は、1つの配列に移動しますが、2番目に移動します) 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
)
4

3 に答える 3

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

}

これにより、質問のわかりやすい部分が実行されます。

于 2012-11-15T14:55:09.070 に答える
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']; 

 }
于 2012-11-15T15:03:33.337 に答える
0

ログ エントリを並べ替えるには、foreach ループといくつかの条件付きステートメントを使用する必要があります。

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

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

好みに応じて条件ステートメントを調整します。

于 2012-11-15T14:50:48.513 に答える