简体   繁体   中英

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.

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 :

$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.

foreach loops over year_2014 data arrays and sets item's amounts to the month - 1 position.

array_map does the same as foreach and can be an overkill here, but also works well.

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 />';
}
  • mktime() will show a timestamp for selected month.
  • date() with param 'M' will show months name in 3 characters : Jan, Feb...

HTML version :

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 . '&nbsp;' : $lst_amount[$month] . '&nbsp;';

// 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. 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

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM