I have code to read excel file in PHP. I can display all rows, but I need to put it to array in specific (for me) way.
My data is:
row1: 1, model1a, 2, 124
row2: 2, model2, 1, 63
row3: 1, model1b, 1, 525
row4: 1, model1c, 4, 1100
row[0] - it is column 0 of given row
row[1] - column 1 of his row, etc.
I want to create array in this way (pseudocode):
$payment_data = array();
for(row : rows){
if( !isset( $payment_data[row[0]] )){
// there is no array for key row[0], i.e. for row1 $payment_data[1], so create it
$payment_data[row[0]] = array('income' => array(), 'model' => array(), 'amount' => array() );
// set values in this array
$payment_data[row[0]]['income'][0] = row[1];
$payment_data[row[0]]['amount'][0] = row[2];
$payment_data[row[0]]['model'][0] = row[3];
}else{
// array already created and have one value
// WHAT SHOULD BE HERE FOR ROW3 AND ROW4, SO INDEX IS AGAIN 1 AND ARRAY ALREADY EXISTS
}
What should be in ELSE statement to add next elements to array for given key?
For this example the result should be like:
$payment_data has 2 elements with index 1 (from row1, row3 and row4) and 2 (from row 2). $payment_data[1] should have 3 antries:
$payment_data[1]:
income[0] = 124;
model[0] = model1a;
amount[0] = 2;
income[1] = 525;
model[1] = model1b;
amount[1] = 1;
income[2] = 1100;
model[2] = model1c;
amount[2] = 4;
$payment_data[2]:
income[0] = 63;
model[0] = model2;
amount[0] = 1;