Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 压缩、重组和添加子阵列_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 压缩、重组和添加子阵列

Php 压缩、重组和添加子阵列,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我一直在挠头,在提出解决我的数组结构问题的方案时惨遭失败。我不确定究竟哪一部分更适合尝试和修复,数据是在事后从SQL或PHP数组返回的 我的SQL数据返回如下: $i = 0; while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) { $colData[$i] = array( 'name' => $row['FULLNAME'],

我一直在挠头,在提出解决我的数组结构问题的方案时惨遭失败。我不确定究竟哪一部分更适合尝试和修复,数据是在事后从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',
    ),
    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',
  ),
结果数组具有以下结构:

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',
  ),
请注意,我正在为同一个人获取一个新的子数组,因为子数组(服务、日期和服务金额)有多个值。 我试图实现的是压缩数组,这样我只有一个“ABRAHAM PRETORIS”等的数组,但所有不同的服务都列为子数组。我希望它看起来像这样:

$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',
    ),
    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查询有一个名为发票的订单。这将确保要分组的所有记录都是连续的

然后,您必须创建一个包含一些附加内部逻辑的循环:

// 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;
这是一般的概念:您将创建一个临时数组来保存当前名称,并在其中计算其他数据。一旦名称更改,acummoulated数据将转储到主结果,临时数组被重置,新的acummoulation开始


我现在无法测试代码,因此它可能需要一些修复,但这种方法工作得非常好,我在这里的目的是向您展示这个概念,以便您可以根据自己的具体需要对其进行调整。

我非常感谢您Marcovecchio!这完全有道理。。现在我终于可以完成这份报告了。。lol@MikeThiel字体我很高兴它帮助了你!顺便说一下,我刚刚在if结束大括号之前添加了一行缺失的内容:
$current_name=$row['FULLNAME']。正在将子阵列信息成功添加到主阵列中。。然而,它一次只做一个,并为每个添加创建一个新的主数组。我正在想出一个我认为不难的解决办法。你肯定让我走上了正确的道路!!再次感谢你的帮助。很抱歉,我不小心将$result[]=$temp放在while循环结束之前,而不是之后。再次感谢!最后一个
$result[]=$temp
将把姓氏添加到
$results
数组中。如果你不加上它,你将失去姓氏。要检查这一点,请查看SQL查询,并将其与
$result
数组进行比较。姓氏将丢失!