どのようにループの2つの配列を通って、マッチング/欠損値を見つけます

マックスResnikoff:

私は現在2つの配列を持っています。

$last_12_months - 現在の月から過去12ヶ月間の配列が含まれています

$app12_array_temp - その月に作られた予定の数、月とクエリ結果が含まれています。

私は毎月をループのことができるように、そしてその月に予定数を割り当てる必要があります。

私はループを入れ子にしていますように、私は、結果としてこれを取得しています:

[0,0,0,0,0,0,0,"1",0,0,0,0,0,0,"4",0,0,0,0,0,0,"2",0,0,0,0,0,0,"15",0,0,0,0,0,0,"9",0,0,0,0,0,0,"8",0,0,0,0,0,0,"2",0,0,0,0,0,0,"1",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

私は必要なのです。

[0,0,0,0,1,4,2,15,9,8,2,1]

私の現在のスクリプトは次のようになります。

//Loop through months, and check if appointments happened in that month 
  foreach($last_12_months as $m => $month){
    foreach($app_12_array_temp AS $app){
      if(date("m", strtotime($app['time'])) == date("m", strtotime($month))){
        $app_12_array[] = $app['count'];
        break;
      }else{
        $app_12_array[] = 0;
      }
    }
  }

その月中の予定があるのであれば、それはそうでなければたびに月対応するループイマイチを0追加され、現在、0を追加し、配列に任命回数を追加する必要があります

私は、条件が満たされたときにブレークを使用して試してみましたが、それはそのデータを見つけるまで0アップを返します。

私も持っているtried In_Array()が、ちょうど戻って[0,0,0,0,1,0,0,0,0,0,0,0]

これは、スクリプトは思考がどうあるべきかですが、私はそれがこれを行うために取得する方法を考えることはできません。

Jan -> Any data for this month? -> No, so add 0 for this month to array
Feb -> Any data for this month? -> No, so add 0 for this month to array
Mar -> Any data for this month? -> Yes, so add $app['count'] to array
Apr -> Any data for this month? -> Yes, so add $app['count'] to array
May -> Any data for this month? -> Yes, so add $app['count'] to array

EDIT

$app_12_array_temp = [
    ['time' => '2019-07-27 13:00:00', 'count' => '1'],
    ['time' => '2019-08-26 13:00:00', 'count' => '2'],
    ['time' => '2019-09-06 13:00:00', 'count' => '8'],
    ['time' => '2019-10-22 12:00:00', 'count' => '9'],
    ['time' => '2019-11-21 12:00:00', 'count' => '15'],
    ['time' => '2019-12-27 11:00:00', 'count' => '2'],
    ['time' => '2020-01-22 15:00:00', 'count' => '4'],
    ['time' => '2020-02-12 09:00:00', 'count' => '1'],
];

$last_12_months = ['Feb', 'Jan', 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar'];
antman3351:

新しい配列のキーとして月を使用して、これを試してください

$app_12_array_temp = [
    ['time' => '2019-07-27 13:00:00', 'count' => '1'],
    ['time' => '2019-08-26 13:00:00', 'count' => '2'],
    ['time' => '2019-09-06 13:00:00', 'count' => '8'],
    ['time' => '2019-10-22 12:00:00', 'count' => '9'],
    ['time' => '2019-11-21 12:00:00', 'count' => '15'],
    ['time' => '2019-12-27 11:00:00', 'count' => '2'],
    ['time' => '2020-01-22 15:00:00', 'count' => '4'],
    ['time' => '2020-02-12 09:00:00', 'count' => '1'],
];

$last_12_months = ['Feb', 'Jan', 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar'];


$app_12_array = array_fill_keys( $last_12_months, 0 );
foreach ( $last_12_months as $m => $month )
{
    foreach ( $app_12_array_temp AS $app )
    {
        if ( date( "m", strtotime( $app['time'] ) ) == date( "m", strtotime( $month ) ) )
        {
            $app_12_array[ $month ] = ($app_12_array[ $month ] ?? 0 ) + $app['count'];
            break;
        }
    }
}

var_dump( $app_12_array );

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=14637&siteId=1