[英]php array loop with sub loop
I would like to process an array in PHP to generate a new one. 我想在PHP中处理一个数组以生成一个新数组。
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. 我的问题是,根据销售请求的类型,需要生成1张或多张票证。 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.
如果需要更多票证,唯一的区别是票证号码的通用票号后缀为-1或-2。
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) 我可以在阵列上执行foreach,然后在销售类型上执行IF / ELSE,然后设置所有新的阵列键(除了票证号以外,其他都相同)
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
}
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. 注意:
$ticket[$k]
(如您的代码中)不能多次使用,因为数据将被覆盖。 That's why I used $ticket[$k . '-' . $d]
这就是为什么我使用
$ticket[$k . '-' . $d]
$ticket[$k . '-' . $d]
$ticket[$k . '-' . $d]
to generate one ticket entry for each day of each sale. $ticket[$k . '-' . $d]
以便在每次销售的每一天生成一个票务条目。 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. 由于其他票证数据似乎相同,因此我可以在生成临时票证之前在for循环中进行复制。
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',
),
...
);
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;
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 不幸的是,我没有找到比这更好的复制PHP数组的方法: http : //php.net/manual/de/arrayobject.getarraycopy.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.