將變數數組重設

來源:互聯網
上載者:User
foreach($arr as k1=>$v1){$sql2 = "select cust_no,total=SUM(mount)*snp from ".$t." where po_num = '".$stockno."' and cust_no = '".$v1['cust_no']."' group by cust_no,snp";$result2 = mssql_query($sql2);$b = array();while($row2 = mssql_fetch_array($result2)){$b[] = $row2;}foreach($b as $var){$num = $var['total'];foreach($a as $i=>$dest){if($num == 0)break;if($dest['cust_no'] != $var['cust_no'])continue;if($num >= $dest['part_count']){$num -= $dest['part_count'];$ress[] = $dest;$a[$i]['part_count'] = 0;}else{$dest['part_count'] = $num;$ress[] = $dest;$a[$i]['part_count'] -= $num;$num = 0;}}}print_r($ress);}


如何把變數數組$ress重設?用了unset()還是沒有效果。


回複討論(解決方案)

沒有看到 unse 在哪裡
請說明需求

沒有看到 unse 在哪裡
請說明需求




對應的sql條件及數組$b的值:
col1:select cust_no,total=SUM(mount)*snp from dfl_stock_detail where po_num = 'SU14031300627' and cust_no = '237033AW0A' group by cust_no,snpArray ( [0] => Array ( [0] => 237033AW0A [cust_no] => 237033AW0A [1] => 375 [total] => 375 ) )col2: select cust_no,total=SUM(mount)*snp from dfl_stock_detail where po_num = 'SU14031300627' and cust_no = '237033RC3A' group by cust_no,snpArray ( [0] => Array ( [0] => 237033RC3A [cust_no] => 237033RC3A [1] => 75 [total] => 75 ) )


按照$b值的不同求對應的$ress的值。

但是現在$ress求出來,col2還是col1的結果。

unset 應該是可以用的 你看哈程式是不是跑到那個unset了 ,在那個unset處輸出點東西看看,有啥子反映

沒有看到 unse 在哪裡
請說明需求


//已知數組a:array (  0 =>   array (    0 => '12653627',    'cust_no' => '12653627',    1 => '4069',    'lotno' => '4069',    2 => 19,    'part_count' => 19,  ),  1 =>   array (    0 => '12653627',    'cust_no' => '12653627',    1 => '4071',    'lotno' => '4071',    2 => 146,    'part_count' => 146,  ),  2 =>   array (    0 => '12654172',    'cust_no' => '12654172',    1 => '4071',    'lotno' => '4071',    2 => 855,    'part_count' => 855,  ),  3 =>   array (    0 => '237033AW0A',    'cust_no' => '237033AW0A',    1 => '4409',    'lotno' => '4409',    2 => 300,    'part_count' => 300,  ),  4 =>   array (    0 => '237033AW0A',    'cust_no' => '237033AW0A',    1 => '4410',    'lotno' => '4410',    2 => 141,    'part_count' => 141,  ),  5 =>   array (    0 => '237033RC3A ',    'cust_no' => '237033RC3A ',    1 => '4418',    'lotno' => '4418',    2 => 80,    'part_count' => 80,  ),  6 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4401',    'lotno' => '4401',    2 => 2,    'part_count' => 2,  ),  7 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4402',    'lotno' => '4402',    2 => 1,    'part_count' => 1,  ),  8 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4408',    'lotno' => '4408',    2 => 178,    'part_count' => 178,  ),  9 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4409',    'lotno' => '4409',    2 => 374,    'part_count' => 374,  ),)//數組barray (  0 =>   array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  1 =>   array (    'cust_no' => '237033RC3A',    'total' => 75,  ),)


數組a與數組b匹配,當數組a的cust_no和數量滿足數組b的cust_no和數量時。需要$ress求得的類似結果如下:

Array(    [0] => Array        (            [cust_no] => 237033AW0A            [lotno] => 4409            [part_count] => 300        )    [1] => Array        (            [cust_no] => 237033AW0A            [lotno] => 4410            [part_count] => 75        )    [2] => Array        (            [cust_no] => 237033RC3A            [lotno] => 4418            [part_count] => 75        ))


這樣的話可否實現?或者放在如1#的迴圈中,當數組b值為多少時,對應的數組a的匹配結果為多少?$ress值被賦值後如何重設?

是這個意思?還是有進一步要求?

$a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),); $b = array (  0 =>   array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  1 =>   array (    'cust_no' => '237033RC3A',    'total' => 75,  ),);foreach($b as $d) {  $k = $d['cust_no'];  reset($a);  foreach($a as $r) {    if($k != $r['cust_no']) continue;    $res[] = $r;  }}print_r($res);
Array
(
[0] => Array
(
[cust_no] => 237033AW0A
[lotno] => 4409
[part_count] => 300
)

[1] => Array
(
[cust_no] => 237033AW0A
[lotno] => 4410
[part_count] => 141
)

[2] => Array
(
[cust_no] => 237033RC3A
[lotno] => 4418
[part_count] => 80
)

)

是這個意思?還是有進一步要求?


$a的資料對應#5的資料。

$v1){$sql2 = "select cust_no,total=SUM(mount)*snp from ".$t." where po_num = '".$stockno."' and cust_no = '".$v1['cust_no']."' group by cust_no,snp";$result2 = mssql_query($sql2);$b = array();while($row2 = mssql_fetch_array($result2)){$b[] = $row2;}var_export($b);/*數組b分別對應2筆資料資料1:  array ( 0 => array ( 0 => '237033AW0A', 'cust_no' => '237033AW0A', 1 => 375, 'total' => 375, ),)資料2: array ( 0 => array ( 0 => '237033RC3A', 'cust_no' => '237033RC3A', 1 => 75, 'total' => 75, ), )) */foreach($b as $var){$num = $var['total'];foreach($a as $i=>$dest){if($num == 0)break;if($dest['cust_no'] != $var['cust_no'])continue;if($num >= $dest['part_count']){$num -= $dest['part_count'];$ress[] = $dest;$a[$i]['part_count'] = 0;}else{$dest['part_count'] = $num;$ress[] = $dest;$a[$i]['part_count'] -= $num;$num = 0;}}}print_r($ress);/*目前的結果:第一筆:Array([0] => Array([0] => 237033AW0A[cust_no] => 237033AW0A[1] => 4409[lotno] => 4409[2] => 300[part_count] => 300)[1] => Array([0] => 237033AW0A[cust_no] => 237033AW0A[1] => 4410[lotno] => 4410[2] => 141[part_count] => 75))第二筆:Array([0] => Array([0] => 237033AW0A[cust_no] => 237033AW0A[1] => 4409[lotno] => 4409[2] => 300[part_count] => 300)[1] => Array([0] => 237033AW0A[cust_no] => 237033AW0A[1] => 4410[lotno] => 4410[2] => 141[part_count] => 75))希望得到的資料:第一筆:Array ( [0] => Array ( [cust_no] => 237033AW0A [lotno] => 4409 [part_count] => 300 ) [1] => Array ( [cust_no] => 237033AW0A [lotno] => 4410 [part_count] => 75   )    )第二筆:Array ([0] => Array ([cust_no] => 237033RC3A[lotno] => 4418[part_count] => 75  )  )*/}?>

部分資料補充:

$arr = array ( 0 => array ( 0 => '2014-03-19 11:00', 'over_time' => '2014-03-19 11:00', 1 => 'A2', 'arr_adress' => 'A2', 2 => 'L12F', 'c_type' => 'L12F', 3 => 'SU14031300627', 'po_num' => 'SU14031300627', 4 => '237033AW0A', 'cust_no' => '237033AW0A', 5 => 25, 'mount' => 25, 6 => 15, 'snp' => 15, 7 => 375, 'totals' => 375, 8 => '2014-03-15', 'late_time' => '2014-03-15', ), 1 => array ( 0 => '2014-03-19 11:00', 'over_time' => '2014-03-19 11:00', 1 => 'A2', 'arr_adress' => 'A2', 2 => 'L12F', 'c_type' => 'L12F', 3 => 'SU14031300627', 'po_num' => 'SU14031300627', 4 => '237033RC3A', 'cust_no' => '237033RC3A', 5 => 5, 'mount' => 5, 6 => 15, 'snp' => 15, 7 => 75, 'totals' => 75, 8 => '2014-03-15', 'late_time' => '2014-03-15', ), ) $a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),);

我 #6 的不符合你的要求嗎?
如果是要對 $b 分組對待,則
foreach($b as $d) {
$res = array();

我 #6 的不符合你的要求嗎?
如果是要對 $b 分組對待,則
foreach($b as $d) {
$res = array();



需要對$a進行減處理,以滿足$b的結果。例如:
$ress的結果
Array ( [0] => Array ( [cust_no] => 237033AW0A [lotno] => 4409 [part_count] => 300 ) [1] => Array ([cust_no] => 237033AW0A [lotno] => 4410 [part_count] => 75 ) )
滿足$b的結果
array ( 0 => array ( 'cust_no' => '237033AW0A', 'total' => 375, ), )

我 #6 的不符合你的要求嗎?
如果是要對 $b 分組對待,則
foreach($b as $d) {
$res = array();

我 #6 的不符合你的要求嗎?


如果是求當$a和$b的cust_no欄位相同時,$a的part_count的數量滿足$b的total的數量怎麼求?

我 #6 的不符合你的要求嗎?



link

和這個問題類似。不過$b變成了多維陣列。

在那個文章中 $b 也是多維陣列

在那個文章中 $b 也是多維陣列


但是只能求得$b中第一個一維數組的結果集。

這個意思?

$a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),);  $b = array (  array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  array (    'cust_no' => '237033RC3A',    'total' => 75,  ),); foreach($b as $source) {  do {    $k = 0;    $num = $source['total'];    foreach($a as $i=>$dest) {      if($num == 0) break;      if($dest['cust_no'] != $source['cust_no']) continue;      if($dest['part_count'] == 0) continue;      $k++;      if($num >= $dest['part_count']) {        $num -= $dest['part_count'];        $res[] = $dest;        $a[$i]['part_count'] = 0;      }else {        $dest['part_count'] = $num;        $res[] = $dest;        $a[$i]['part_count'] -= $num;        $num = 0;      }    }  }while($k);}var_export($res);
array (  0 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  1 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 75,  ),  2 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 66,  ),  3 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 75,  ),  4 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 5,  ),)

這個意思?



如果只求得這樣的結果集:怎麼求?

array (  0 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  1 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 75,  ),  2 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 75,  ),)

這個意思?



可能是哪裡的程式問題,同樣的代碼,程式的和例子得出來的結果不一樣。我先調試調試再說吧。謝謝!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.