Question

I am confused and i am new to php. I am using this code to get the list whose status is Unpaid and time taken(in days) is >30 or >60 or >90 days. If time taken is >30 days then color should be blue, >60 days then color should be orange and if >90days then color should be red. All condition are working but problem is i am not getting array in proper format how i want.

My code is

foreach ($listview_entries as $key => $value){
    $invoice_focus = new Invoice();
    $invoice_focus->id = $key;
    $invoice_focus->retrieve_entity_info($key, "Invoice");
    $invoicestatus = $invoice_focus->column_fields['invoicestatus'];
    $invoicedate = $invoice_focus->column_fields['invoicedate'];
    $currentdate = date("Y-m-d");
    $start_date2 = strtotime($invoicedate);
    $end_date2 = strtotime($currentdate);
    $difference = $end_date2 - $start_date2;
    $days = floor($difference/86400);
    $items = array();
    $color1 = array();
    if($invoicestatus == 'Unpaid' && $days>30)
    {
        if($days>60)
        {
            if($days>90)
            {
                $milan=array($key);
                $color=array_fill_keys($milan, 'red');
                //$smarty->assign('COLOR', $color);
            }
            else
            {
                $milan=array($key);
                $color=array_fill_keys($milan, 'orange');
                //$smarty->assign('COLOR', $color);
            }
        }
        else
        {
            $milan=array($key);
            $color=array_fill_keys($milan, 'blue');
            //$smarty->assign('COLOR', $color);
        }
        if(!empty($color)){
        array_push($color1,$color);
        }
        print_r($color1);
    }
    $smarty->assign('COLOR', $color1);
}

And i am getting output like this.

Array ( [0] => Array ( [89] => red ) ) Array ( [0] => Array ( [91] => blue ) ) Array ( [0] => Array ( [92] => orange ) ) 

But i want like this.

Array ( [0] => Array ( [89] => red ),[1] => Array ( [91] => blue ),[2] => Array ( [92] => orange ) )

Help me please.

Was it helpful?

Solution

Please Remove array reserved key form following line:

$milan=array($key);

So, put code like this

$color [$key] = 'red';

instead of

$milan=array($key);
$color=array_fill_keys($milan, 'red');

OTHER TIPS

Update to this one.

From :

array_push($color1,$color);
$items = array($color1);

To

array_push($color1, $color);
if(!empty($color1)) {
    $items[] = $color1;
}

And $items = array() before loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top