比较两个不同的数组,并在PHP中的不同记录中以零输出

比较两个不同的数组,并在PHP中的不同记录中以零输出,php,arrays,Php,Arrays,我有两个不同的数组,如下所示 月数组 0 :November 1 :October 2 :September 3 :August 4 :July 5 :June 数据阵列 0 { profit :4045 month :June } 1 { profit :1161 month :August } 2 { profit :730 month :October } 3 { profit

我有两个不同的数组,如下所示

月数组

0   :November
1   :October
2   :September
3   :August
4   :July
5   :June
数据阵列

0   {
   profit   :4045
   month    :June
}
1   {
   profit   :1161
   month    :August
}
2   {
   profit   :730
   month    :October
}
3   {
   profit   :1700
   month    :November
}
我需要比较这两个数组,并将
利润
作为
0
创建新数组。最后我想得到下面的数组

0   {
   profit   :4045
   month    :June
}
1   {
   profit   :0
   month    :July
}
2   {
   profit   :730
   month    :August
}
3   {
   profit   :0
   month    :September
}
4   {
   profit   :1700
   month    :October
}
5   {
   profit   :1700
   month    :November
}
我试过下面的代码,但不起作用

foreach ($month as $key => $value) {
  if (array_key_exists($key, $data->month) {
    $month[$key] = $data[$key];
  } else {
    $month[$key] = 0;
  }
}

我如何使用
month
data
数组创建此数组?

foreach
循环应该可以做到这一点

$month = array('November', 'October', 'September', 'August', 'July', 'June');
$data = array(
        array('profit' => 4045, 'month' => 'June'),
        array('profit' => 1161, 'month' => 'August'),
        array('profit' => 730, 'month' => 'October'),
        array('profit' => 1700, 'month' => 'November')
    );

// output and temp arrays
$output = [];
$temp = [];

// Loop thru each data and set month as key, profit as value
foreach ($data as $value) {
    $temp[$value['month']] = $value['profit'];
}

// Reverse month array
$month = array_reverse($month, true);

// Loop thru each month, check if month exist on temp, if not profit stays 0
foreach ($month as $value) {
    $profit = 0;
    if (array_key_exists($value, $temp)) {
        $profit = $temp[$value];
    }
    $output[] = array('profit' => $profit, 'month' => $value);
}

// Output
echo '<pre>';
print_r($output);
$month=array('11月'、'10月'、'9月'、'8月'、'7月'、'6月');
$data=数组(
数组('profit'=>4045,'month'=>June'),
数组(“利润”=>1161,“月”=>8月”),
数组(“利润”=>730,“月”=>10月”),
数组('利润'=>1700,'月份'=>11月')
);
//输出和临时阵列
$output=[];
$temp=[];
//循环遍历每个数据,并将月份设置为键,将利润设置为值
foreach(数据为$value){
$temp[$value['month']=$value['profit'];
}
//反月数组
$month=array\u reverse($month,true);
//循环每个月,检查temp上是否存在月份,如果没有,利润保持0
foreach(月为$value){
美元利润=0;
如果(数组\键\存在($value,$temp)){
$PROPERIT=$temp[$value];
}
$output[]=array('利润'=>$PROFICE,'月份'=>$value);
}
//输出
回声';
打印(输出);

您的问题显示出解决问题的能力。如果您尝试过,您应该编辑我们的问题,详细说明您所做的工作、研究的目的,并指向任何有帮助但没有回答您问题的链接。如果您试图编写解决方案,则应将其添加到编辑中。你的尝试应该转变成一个清晰的阅读和理解。也请阅读《非常感谢》,它很有效。你救了我的命:)