Welcome Guest, Not a member yet? Register   Sign In
To those array experts
#9

In theory, you could continue down this path and, eventually, you might get the result you're looking for, but it's likely that this code will be impossible to maintain.

What I would recommend is that you clarify what data you're looking for and define a set of variables with meaningful names before the loop. Also before the loop, determine what data you need from the database and get it (emphasis on before the loop). The data from the database should also be stored in variables with meaningful names. From the looks of what you're doing here, it might even be easier to get the data you're looking for with a join instead of making two separate queries and looping through all of the results.

Make sure you're consistent with the way you treat your data. For example, you use $uid twice in your code, but in one case you pass it to where_in() while in the other you pass it to where() (while the methods are on two different classes, I'm assuming they're at least somewhat consistent with $this->db->where() and $this->db->where_in()).

$customer_id_count is not going to change inside your loop (with the current code), so you can safely get it out of the way by moving it out of the loop.

In this section:
PHP Code:
$data['item_count_item'] = $items->result_count();  //1
$data['items'] = $items;  
//$data['dates3'] = array();
$data['dates2'] = array();
$data['dates'] = array();
$data['total'] = array();
$data['total_cost'] = array();
$data['total_for_cost'] = array(); 

You initialize a bunch of arrays, eliminating whatever was stored in them on the previous run through the loop. Then, after looping through $dates, you copy and manipulate a bunch of data and store it in those arrays, right before the end of the loop:

PHP Code:
$data['dates2'][$i]=$data['dates'];  
$data['dates3'][$i]=$data['dates2'][$i];  
$data['total_for_cost'] = array_reduce($data['total'], create_function('$total, $next''$total += $next["total_cost"]; return $total;')); 

So, these last 3 assignments are meaningless for all but the last iteration of the loop, as are $data['item_count_item'], $data['items'], $data['dates'], $data['total'], and $data['total_cost'].

You have the following:
PHP Code:
foreach ($items as $item) {
    for(
$count 0$count $customer_id_count[$item->id]; $count++) {
        
$item->id;
        if (
$item->id == $new_date) {
            
$data['dates'][$date->id]['jobs'] = array(
                
'name' => $item->support_item
            
);
        }
    }


The for loop in this section is, at best, just wasting cycles. At worst, it might be preventing the loop from ever satisfying the $item->id == $new_date condition. The $item->id call inside the for loop isn't doing anything, either (unless you have a magic method doing something in the class).
Reply


Messages In This Thread
To those array experts - by scoobie - 08-22-2015, 11:47 PM
RE: To those array experts - by jLinux - 08-23-2015, 12:21 AM
RE: To those array experts - by scoobie - 08-23-2015, 06:01 AM
RE: To those array experts - by pdthinh - 08-23-2015, 10:02 AM
RE: To those array experts - by scoobie - 08-23-2015, 04:20 PM
RE: To those array experts - by RobertSF - 08-23-2015, 07:26 PM
RE: To those array experts - by scoobie - 08-24-2015, 02:03 AM
RE: To those array experts - by RobertSF - 08-24-2015, 10:57 AM
RE: To those array experts - by mwhitney - 08-24-2015, 02:18 PM
RE: To those array experts - by scoobie - 08-26-2015, 03:46 PM
RE: To those array experts - by scoobie - 08-26-2015, 11:17 PM
RE: To those array experts - by RobertSF - 08-27-2015, 09:00 AM
RE: To those array experts - by mwhitney - 08-27-2015, 01:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB