thenorman138 Posted April 10, 2018 Share Posted April 10, 2018 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 More sharing options...
requinix Posted April 10, 2018 Share Posted April 10, 2018 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 More sharing options...
thenorman138 Posted April 10, 2018 Author Share Posted April 10, 2018 I'm trying that but it says that argument #2 for array_replace is not an array? Link to comment Share on other sites More sharing options...
requinix Posted April 10, 2018 Share Posted April 10, 2018 Initialize $skuTots to an empty array before the loop. Link to comment Share on other sites More sharing options...
thenorman138 Posted April 10, 2018 Author Share Posted April 10, 2018 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 More sharing options...
requinix Posted April 10, 2018 Share Posted April 10, 2018 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 More sharing options...
thenorman138 Posted April 10, 2018 Author Share Posted April 10, 2018 (edited) 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 April 10, 2018 by thenorman138 Link to comment Share on other sites More sharing options...
requinix Posted April 10, 2018 Share Posted April 10, 2018 Alright. Let's take everything over to your other ongoing thread. Sure, you might have a different question each time, but splitting the work over a bunch of threads is counterproductive. Link to comment Share on other sites More sharing options...
Recommended Posts