[英]How to echo value from an array in according to a key value in PHP
Please help me with this array printing. 请帮助我进行此阵列打印。
Array 数组
[year_2014] => Array
(
[0] => Array
(
[amount] => 21960
[year] => 2014
[month] => 1
)
[1] => Array
(
[amount] => 25866
[year] => 2014
[month] => 2
)
[2] => Array
(
[amount] => 7840
[year] => 2014
[month] => 3
)
[3] => Array
(
[amount] => 424644
[year] => 2014
[month] => 5
)
[4] => Array
(
[amount] => 22052
[year] => 2014
[month] => 6
)
[5] => Array
(
[amount] => 28037
[year] => 2014
[month] => 7
)
)
I need to echo amount in according to month so the Output will be, 我需要根据月份回显金额 ,以便输出为
Result 结果
21960, 25866, 7840, 0, 424644, 22052, 28037, 0, 0, 0, 0, 0
That is if a month is not present then the value need to be zero,I need all the twelve month. 也就是说,如果一个月不存在,则该值需要为零,我需要全部十二个月。
My dear Good Hearts please help me to get this result. 亲爱的善良的心,请帮助我获得这个结果。
Some background 一些背景
project is done in codeigniter , I have messed with some for, foreach but it's not working. 项目是在codeigniter中完成的,我已经为foreach搞砸了一些,但是没有用。
Thank you. 谢谢。
Just try with: 只需尝试:
$output = array_fill(0, 12, 0);
array_map(function ($item) use (&$output) {
$output[$item['month'] - 1] = $item['amount'];
}, $input['year_2014']);
or with simple foreach
: 或使用简单的
foreach
:
$output = array_fill(0, 12, 0);
foreach ($input['year_2014'] as $item) {
$output[$item['month'] - 1] = $item['amount'];
}
Output: 输出:
array (size=12)
0 => int 21960
1 => int 25866
2 => int 7840
3 => int 0
4 => int 424644
5 => int 22052
6 => int 28037
7 => int 0
8 => int 0
9 => int 0
10 => int 0
11 => int 0
Explanation: 说明:
array_fill
creates an array with 12 elements filled with 0
values. array_fill
创建一个数组,其中包含12个元素,并填充了0
值。
foreach
loops over year_2014
data arrays and sets item's amounts to the month - 1
position. foreach
在year_2014
数据数组上循环,并将项目的金额设置为month - 1
位置。
array_map
does the same as foreach
and can be an overkill here, but also works well. array_map
与foreach
一样,在这里可能是一个过大的array_map
,但效果很好。
You can loop over each month : 您可以每月循环:
$monthsList = array();
foreach( $year_2014 as $months)
$monthsList[$months['month']] = $months['amount'];
print_r($monthsList); // will output array(1 => 21960, 2 => 21960 ...)
for ( $i = 1 ; $i <= 12 ; $i++ )
{
// You check if an amount exists for this month.
$amount = !empty($monthsList[$i]) ? $monthsList[$i] : 0;
// You can show date with mktime() to get a timestamp and date() to show month name.
echo date('M', mktime(0, 0, 0, $i, 1, 2014)).' -> '.$amount.'<br />';
}
HTML version : HTML版本:
echo '<table>';
echo '<tr>';
for ( $i = 1 ; $i <= 12 ; $i++ )
echo '<th>'.date('M', mktime(0, 0, 0, $i, 1, 2014)).'</th>';
echo '</tr>';
echo '<tr>';
for ( $i = 1 ; $i <= 12 ; $i++ )
echo '<td>'.(!empty($monthsList[$i]) ? $monthsList[$i] : 0).'</td>';
echo '</tr>';
echo '</table>';
$lst_amount = array();
foreach ($arr_year_2014 as $arr_month)
$lst_amount[$arr_month["month"]] = $arr_month["amount"];
// Output
foreach (range(1, 12) as $month)
echo empty($lst_amount[$month]) ? 0 . ' ' : $lst_amount[$month] . ' ';
// Output as HTML table
$html = "<table>\n<tr>\n";
foreach (range(1, 12) as $month) {
$monthName = date('M', mktime(0, 0, 0, $month, 10));
$html .= "<th>$monthName</th>\n";
}
$html .= "</tr>\n<tr>\n";
foreach (range(1, 12) as $month) {
$amount = empty($lst_amount[$month]) ? 0 : $lst_amount[$month];
$html .= "<td>$amount</td>\n";
}
$html .= "</tr>\n</table>\n";
echo $html;
sorting: 排序:
<?php
function cmp($a, $b)
{
return $a['month'] > $b['month'];
}
//$data = your array
usort($data, "cmp");
foreach ($data as $value) {
echo $value['amount'];
}
?>
You want to loop through each numeric month (1 - 12), and check if any of the nodes in the array have that month. 您要遍历每个数字月份(1-12),并检查数组中是否有任何节点具有该月份。 So in psuedo-code:
所以在伪代码中:
for ( months ) { // 1 through 12
for ( each node in year_array ) {
if ( month == currentMonth ) {
print value of month
}
else
print 0
}
}
}
Create static array for month as 为月份创建静态数组为
$months = array('1'=>'Jan','2'=>'Feb',.....'12'=>'Dec');
then loop with foreach 然后与foreach循环
$num_months = array_keys($months);
foreach($year_array as $key => $amount_array)
{
if(in_array($amount_array['month']),$num_months)
echo $amount_array['amount'];
else
echo "0";
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.