我一直在挠我的头,悲惨地失败在提出一个解决方案,我的数组结构问题。我不确定究竟是哪一部分会更好地尝试和修复,从SQL或PHP数组返回的数据之后的事实。
我的SQL数据返回如下:
$i = 0;
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) {
$colData[$i] = array(
'name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
array(
'service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553'],
),
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
$i++;
}
生成的数组结构如下:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
1 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
2 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Stumps',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
注意,我得到了同一个人的新子数组,因为子数组(service, date &Service_amount)有多个值。我想要完成的是压缩数组,这样我就只有一个数组"ABRAHAM PRETORIS"等,但所有不同的服务都被列为子数组。我希望它看起来像这样:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
1 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
2 =>
array (
'service' => 'STUMPS',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
我看过很多嵌套foreach语句和php数组函数的例子,但我就是不知道如何循环并将额外的服务添加到数组中,然后在它是具有不同名称和/或发票号的行时继续。提前感谢您的帮助!!
首先,确保您的SQL查询具有order by name, invoice
。这将确保您想要分组的所有记录都是顺序的。
然后你必须用一些额外的内部逻辑创建一个循环:
// Creates an array to hold the final array.
$result = array();
// This var will keep track of name changes.
$current_name = '';
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) )
{
// Let's check if the name changed. This will be true for the first
// time the loop runs.
if($current_name != $row['FULLNAME'])
{
// If we are beginning, the if below will not run. But in subsequent
// records, it will add the acumulated array to the main result.
if($current_name != '') $result[] = $temp;
// The temp array will be populated with all data that DOES NOT change
// for the current name.
$temp = array('name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
// Update the current name.
$current_name = $row['FULLNAME'];
}
// The part that runs only on name changes has finished. From now on, we
// will take care of data which will be accumulated
// in a sub-array (until name changes and the block above resets it).
$temp['sub-array'][] =
array('service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553']);
}
// After the loop, the last temp array needs to be added too.
$result[] = $temp;
这是一般的概念:您将创建一个临时数组来保存当前名称,并在其中累积其他数据。一旦名称更改,累积的数据将被转储到主结果中,temp数组将被重置,并开始新的累积。
我现在不能测试代码,所以它可能需要一些修复,但这种方法非常有效,我在这里的目的是向您展示这个概念,以便您可以根据您的特定需求进行调整。