Reputation: 124
I would like to process an array in PHP to generate a new one.
The case is that I have an array with sales request and want to generate ticket request from that.
The issue I have is that depending on the type of sales request, 1 or more tickets are requered to be generated. If more tickets are requered, the only difference is the ticket number has a suffix of -1 or -2 added to the general ticket number.
I could do a foreach on the array and then a IF / ELSE if on the sales type and then set all the new array keys (which are all the same except for the ticket number)
But because I have many sales requests / lines this would be hard to maintain and I think not good for performance.
Example "semi" code:
$ticket = array ();
foreach ( $input as $k=>$v )
{
$ticket[$k] ['exp_date'] = date('Y-m-d', strtotime('+1 year', strtotime($input[$k] ['sales_date'])) );
$ticket[$k] ['ticket_number'] = input[$k] ['ticketnumber']; // if 1 day, ticket number, if 2 days ticket number + '-2' If multiple tickets are used, the first ticket should also have '-1'
$ticket[$k] ['ticket_type'] = $input[$k] ['product'];
$ticket[$k] ['sales_date'] = $input[$k] ['sales_date'];
$ticket[$k] ['sales_ticket_number'] = $input[$k] ['ticket_number'] ;
$ticket[$k] ['days'] = '0'; // not yet in use
$ticket[$k] ['days_remaining'] = '0'; // not yes in use
}
// if
// if ($input[$k] ['product'] == '1-day') { $loop is null}
// elseif ($input[$k] ['product'] == '2-days') { $loop is 2}
// elseif ($input[$k] ['product'] == '3-days') { $loop is 3}
Suggestions / tips are much appreciated !
/EDIT
Flow logic without code and sub-optimal:
foreach ($input as $k=>$v)
{
if ( $input[$k] ['product'] == '1-day')
{
create new lines in new array
}
else if ( $input[$k] ['product'] == '2-days')
{
loop 2 times
create same entries, but ticket number = ticketnumber-1 and second loop ticketnumber-2
}
Upvotes: 0
Views: 173
Reputation: 9022
From what I understand, you want something like this:
$ticket = array ();
foreach ( $input as $k=>$v )
{
// add the shared data for all tickets of one product to temporary ticket
$temp_ticket['exp_date'] = date('Y-m-d', strtotime('+1 year', strtotime($input[$k] ['sales_date'])) );
$temp_ticket['ticket_type'] = $input[$k]['product'];
$temp_ticket['sales_date'] = $input[$k]['sales_date'];
$temp_ticket['sales_ticket_number'] = $input[$k]['ticket_number'] ;
$temp_ticket['days'] = '0'; // not yet in use
$temp_ticket['days_remaining'] = '0'; // not yes in use
// get 'product' and retrieve the number of days
$days = (int) $input[$k]['product']; // '1-day' becomes 1, '2-days' becomes 2, ...
for ($d = 1; $d <= $days; $d++) {
// loop through the number of days and add to ticket array
// creates one ticket for each day of a product
$ticket[$k . '-' . $d] = $temp_ticket;
$ticket[$k . '-' . $d]['ticket_number'] = input[$k]['ticketnumber'] . '-' . $d;
}
}
Notes: $ticket[$k]
(as in your code) can't be used multiple times since the data would be overwritten. That's why I used $ticket[$k . '-' . $d]
to generate one ticket entry for each day of each sale. Since the rest of the ticket data seems to be the same, I can generate the temp ticket before and make copies in the for loop.
Example of input:
$input = array(
'order_1234' => array(
'sales_date' => '2013-12-31',
'product' => '1-day',
'ticket_number' => '1234',
),
'order_5678' => array(
'sales_date' => '2014-03-31',
'product' => '3-days',
'ticket_number' => '5678',
),
...
);
My code would produce output like this
$ticket = array(
'order_1234-1' => array(
'exp_date' => '2014-12-31',
'sales_date' => '2013-12-31',
'ticket_type' => '1-day',
'sales_ticket_number' => '1234',
'days' => '0',
'days_remaining' => '0',
'ticket_number' => '1234-1',
),
'order_5678-1' => array(
'exp_date' => '2015-03-31',
'sales_date' => '2014-03-31',
'ticket_type' => '3-days',
'sales_ticket_number' => '5678',
'days' => '0',
'days_remaining' => '0',
'ticket_number' => '5678-1',
),
'order_5678-2' => array(
'exp_date' => '2015-03-31',
'sales_date' => '2014-03-31',
'ticket_type' => '3-days',
'sales_ticket_number' => '5678',
'days' => '0',
'days_remaining' => '0',
'ticket_number' => '5678-2',
),
'order_5678-3' => array(
'exp_date' => '2015-03-31',
'sales_date' => '2014-03-31',
'ticket_type' => '3-days',
'sales_ticket_number' => '5678',
'days' => '0',
'days_remaining' => '0',
'ticket_number' => '5678-3',
),
...
);
Upvotes: 1
Reputation: 2507
You can create an array holding all elements except the ticket number.
Then for each ticket you copy this array and add the ticket number.
Unfortunately I did not find a better way to copy a PHP array than this: http://php.net/manual/de/arrayobject.getarraycopy.php
Upvotes: 0
Reputation: 9041
If I understand the question correctly, which i may not have done you could do:
$prod = strpos($input[$k] ['product'], "-");
if ($prod == '1'):
$loop is null;
else:
$loop is $prod;
endif;
Upvotes: 0