在php中更新多维索引数组

时间:2016-11-22 00:36:59

标签: php arrays merge

所以,我不确定这是否是最好的方法,但我正在为将从数据库中获取数据的客户构建一个Web应用程序(股票名称,股票价格,用户购买股票,购买日期)并显示每个月购买股票的股票价格X.我遇到的问题是eaxh股票有单独的条目。因此,例如,如果用户在4月份购买了facebook股票,并且在5月再次购买了facebook股票,则会创建两条记录。我希望能够将数据合并到一个表格中的单行中,该表格显示每个股票的月度购买价格,方法是将每个月的值组合成一个代表该股票的行。

我已尝试过各种方法来使用数组,但无法找到基于股票名称合并数组的方法。 php有办法做到这一点吗?

以下是代码:

<?php
// TEMPLATE NAME: TEST REPORTS
get_header();
$get_contributions = 'y';
$get_withdrawls = 'y';
$run_report = 'y';
$i = 0;
global $wpdb;
// GET USER DATA
$get_user_data = "SELECT * FROM $wpdb->users";
$get_user_data_results = $wpdb->get_results($get_user_data);
?>
<table style="width: 98%; margin: 40px auto;"><tbody style="width:100%;">
<tr style="text-align:center;">
<td style="width: 7%;">Name</td><td style="width: 7%;">January</td><td   
style="width: 7%;">February</td><td style="width: 7%;">March</td>
<td style="width: 7%;">April</td><td style="width: 7%;">May</td><td
style="width: 7%;">June</td><td style="width: 7%;">July</td><td    
style="width: 7%;">August</td>
<td style="width: 7%;">September</td><td style="width:  
7%;">October</td><td style="width: 7%;">November</td><td style="width:   
7%;">December</td>
</tr>
<?php
// LOOP THROUGH USER DATA 
foreach($get_user_data_results as $user_results):

$user_id = $user_results->ID;
$get_stock_data = "SELECT * FROM nli_user_stocks WHERE user_id   
=".$user_id;
$get_stock_results = $wpdb->get_results($get_stock_data);

if($get_contributions == 'y'){
// SET VARIABLES FOR MONTHLY CONTRIBUTIONS   

$company_stock_symbol = array();

foreach($get_stock_results as $stock_results):

$month = $stock_results->month;
$purchase_price = $stock_results->purchase_price;
$stock_symbol = $stock_results->stock_symbol;
$company_name = $stock_results->company_name;
$company_stock_symbol_size = count($company_stock_symbol);

$jan = '';
$feb = '';
$mar = '';
$apr = '';
$may = '';
$jun = '';
$jul = '';
$aug = '';
$sept = '';
$oct = '';
$nov = '';
$dec = '';


switch ($month) {
case 1:
$jan = $purchase_price;
break;
case 2:
$feb = $purchase_price; 
break;
case 3:
$mar = $purchase_price;
break;
case 4:
$apr = $purchase_price;
break;      
case 5:
$may = $purchase_price;
break;  
case 6:
$jun = $purchase_price;
break;              
case 7:
$jul = $purchase_price;
break;  
case 8:
$aug = $purchase_price;
break;      
case 9:
$sept = $purchase_price;
break;  
case 10:
$oct = $purchase_price;
break;              
case 11:
$nov = $purchase_price;
break;  
case 12:
$dec = $purchase_price;
break;                      
default:
$jan = $purchase_price;
} 

$add_array_values = array($stock_symbol, $month, $purchase_price, $jan,  
$feb, $mar, $apr, $may, $jun, $jul, $aug, $sept, $oct, $nov, $dec,    
$company_name);
array_push($company_stock_symbol, $add_array_values);

endforeach;
} // endif

foreach($company_stock_symbol as $stock_infoB):

if($i%2==true){
echo '<tr style="text-align:center;">';
}else{
echo '<tr style="text-align:center; background: #ded;">';
}    
echo'        
<td style="width: 16%; text-align: left; padding: 15px 0 15px 10px;
background: #444; color: #fff;">'.$stock_infoB[15].'</td><td    
style="width: 7%;">'.$stock_infoB[3].'</td>
<td style="width: 7%;">'.$stock_infoB[4].'</td><td style="width:   
7%;">'.$stock_infoB[5].'</td><td style="width: 
7%;">'.$stock_infoB[6].'</td><td style="width: 
7%;">'.$stock_infoB[7].'</td>
<td style="width: 7%;">'.$stock_infoB[8].'</td><td style="width:   
7%;">'.$stock_infoB[9].'</td><td style="width:  
7%;">'.$stock_infoB[10].'</td><td style="width: 
7%;">'.$stock_infoB[11].'</td>
<td style="width: 7%;">'.$stock_infoB[12].'</td><td style="width: 
7%;">'.$stock_infoB[13].'</td><td style="width: 
7%;">'.$stock_infoB[14].'</td>
</tr>';
$i++;
endforeach;
endforeach;?>
</tbody></table>
<?php
get_footer();
?>

其中输出以下内容:

Stock Reports Page

我需要将facebook行作为一行。

1 个答案:

答案 0 :(得分:0)

感谢所有帮助。我最后在新创建的桌子上使用了JOIN,经过一整天没有进展,今天在这个社区的帮助下赚了一大笔。