Seperating Multidimentional Array

So here goes,

I have a result from a query that i cannot sort using the query function so i have out put into an array within a while loop

$invResults[$x] = array(‘Vendor’=>$VendorType,
‘Type’=>$Type,
‘CustumerCode’=>$CustumerCode,
‘Source’=> $Source ,
‘ISBN’=>$ISBN,
‘Qty’=>$Qty ,
‘BasePrice’ =>$BasePrice ,
‘totalPrice’=>$totalPrice,
‘Cond’ =>$Cond ,
‘InQB’=>$InQB,
‘CSDTotal’=>$CSDTotal,
‘FLTTotal’=>$FLTTotal,
‘GMTotal’=>$GMTotal,
‘TXTTotal’=>$TXTTotal,
‘NEBBIDSTotal’=>$NEBBIDSTotal,
‘RemTotal’=>$RemTotal,
‘totalFinal’=>$totalFinal
);
$x++;
i need this information the way it is as a full array to output to an excel file . and that is working fine.

but now i need to take this array and separate it into multiple arrays based on ‘CustumerCode’…

Which i can do with
$temp_array = [];
foreach ($invResults as $init) {
// Initially, group them on the id key as sub-arrays
$temp_array[$init[‘CustumerCode’]][] = $init;
}
what i need to do is assign each value of ‘CustumerCode’ to its own array and be able to have the system name the array in a way that i can use it later in the file to do some output and calculations.

for example if the CustomerCode is NN then all of the rows of the original array ($invResults) need to be put into an array named $NNarray. The CustomerCode will change often and can have up to 20 different values in the original array. i need it to be able to put all matching CustomerCodes into 1 array and make as many arrays as there are CustomerCodes. And a way to recall the name of the array ( the structure of the arrays will all be the same as the original.

Why not?

Sounds like PDO fetch group might be what you need
Example:

$data = $pdo->query('SELECT sex, name, car FROM users')->fetchAll(PDO::FETCH_GROUP);
array (
  'male' => array (
    0 => array (
      'name' => 'John',
      'car' => 'Toyota',
    ),
    1 => array (
      'name' => 'Mike',
      'car' => 'Ford',
    ),
  ),
  'female' => array (
    0 => array (
      'name' => 'Mary',
      'car' => 'Mazda',
    ),
    1 => array (
      'name' => 'Kathy',
      'car' => 'Mazda',
    ),
  ),
)

yes that would work if the results from the query where the only thing in the output and i was sorting or grouping by what is output by the query --there are hundreds of lines of code that produce the $invResults array–sorry i should have put that in the question.

Doesn’t sound right. Can you PM me a zip of your code base so I can run it as you do?

I would say…
Use objects

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service