Jump to content

array result at wrong level


thenorman138

Recommended Posts

I'm using an array based off of two other arrays in order to get a count of product IDs per customer. The following array is what's printing now:

 

    Array

    (

    [5] => 4

    )

    Array

    (

    [5] => 1

    )

    Array

    (

    [5] => 1

    )

    Array

    ( 

    [5] => 1

    )

    Array

    (

    [5] => 2

    )

 

So the [5] is the product number, the number after => is the quantity per customer (each array line represents a customer).

 

I want to continue having my source array data at the customer level, but what I really want right now is one main array for each product number with totals in the array, so for the above, my desired result is this:

 

    array

    (

    [5] => 9

    )

 

here's what gives me the output I currently have:

 

    


 $skuTots = array_fill_keys(array_unique(array_column($skuResult, 'sku_id')), 0);
foreach ($skuResult as $rec) {
    $skuTots[$rec['sku_id']] += $dealerResult[$rec['dealer_id']];
}
print_r($skuTots);

 

How can I get this desired change?

Link to comment
Share on other sites

The code you've posted is in some other loop. That's why there are so many "Array(...)" outputs instead of just one.

 

You have to rearrange how you're dealing with $skuTots so that it doesn't reset inside the loop. Possibly

$skuTots = array_replace(array_fill_keys(array_unique(array_column($skuResult, 'sku_id')), 0), $skuTots);
Link to comment
Share on other sites

Ok I actually go that fixed but it's still doing the same structure. For instance, it's selecting three rows, all for sku_id 1 and it prints 3 arrays, all for index [1] but different quantities rather than one array that has [1] => 'total'

 

Here's what I'm dealing with:

 

$dealerRslt = odbc_exec($DB2Conn, $dealerQuery);


$dealerResult = [];
$skuTots = [];
foreach($skuResult as $skuRow){
            while($dealerRow = odbc_fetch_array($dealerRslt)){
             $dealerResult[] = $dealerRow;


                if ( !isset($dealerResult[$dealerRow['CSTNOC']]) ) {
                    $dealerResult[$dealerRow['CSTNOC']] = 0;
                }
                $dealerResult[$dealerRow['CSTNOC']] += $dealerRow['TOTALQTY'];


$skuTots = array_replace(array_fill_keys(array_unique(array_column($skuResult, 'sku_id')), 0), $skuTots);


foreach ($skuResult as $rec) {
    $skuTots[$rec['sku_id']] += $dealerResult[$rec['dealer_id']];
}
print_r($skuTots);
            }
        }
Link to comment
Share on other sites

You're still printing the array inside the loop. You're going to see it updating as it goes. If you want to see just the final result then don't print until after the loop finishes.

 

But your code doesn't make sense. Do you need $dealerResult after the loop? If so, how are you using it?

Link to comment
Share on other sites

Yes I will. I've been trying to build this up for a few days and still making sense of it. I'm basically having to take 2 queries from 2 different databases, and I have a source array for each but they're matched up because one database has other attributes that match.

 

Anyway, by the end of this I need to export to a CSV or spreadsheet with some data based on these queries. Most of it comes directly from the first query, but some things are totaled and averaged,which is where this structure comes in.

 

https://forums.phpfreaks.com/topic/307082-combining-data-from-2-arrays-into-final-array-for-csv-output/   This explains my end goal more in depth

Edited by thenorman138
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.