2016-06-23 40 views
0

關於到這些問題:合併一個陣列 - 幾乎不重複值

https://stackoverflow.com/questions/37934938/marge-array-almost-uniques-value

我需要在一個陣列合併值。我有啓動值這樣的:

Array (
    [row] => Array 
     (
     [0] => Array 
      (
      [personalnr] => 824 
      [brutto] => 1000 
      [netto] => 0 
      [notiz] => 0 
      ) 
     [1] => Array 
      (
      [personalnr] => 824 
      [brutto] => 1000 
      [netto] => 0 
      [notiz] => 1 
      ) 
     [2] => Array 
      (
      [personalnr] => 824 
      [brutto] => 1000 
      [netto] => 0 
      [notiz] => 3 
      ) 
     ) 
) 

問題是存在的row[0]row[1]row[2]具有相同[personalnr][brutto][netto][notiz]是每行不同。 假設在我的陣列中,我有400 [rows]。一行重複4次,但每次只有不同的[notiz]。我需要有隻100 [rows],但在每一個 - 需要被列[notiz] 4倍([notiz_1][notiz_2][notiz_3][notiz_4][rows]最終金額100

Array (
    [row] => Array 
    (
     [personalnr] => 824 
     [brutto] => 100 
     [netto] => 0 
     [notiz] => array 
      (
      [1] = > 1 
      [2] = > 2 
      [3] = > 3 

      ) 
    ) 
) 

就像有人說我試過:

foreach ($value as $rownr => $dane) { 
     $nrwiersza = $rownr; 
     $test[$nrwiersza]['personalnr'] = $dane['personalnr']; 
     $test[$nrwiersza]['std'] = $dane['std']; 
     $test[$nrwiersza]["notiz"][] = $dane['notiz']; 


    } 

但是數組$test仍然有400行。如何使用鍵[personalnr],[brutto], [netto]來確定並將每個[notiz]放入[notiz_1][notiz_2][notiz_3][notiz_4]

+1

你的意思是'合併'嗎? – Ray

+2

不要問同樣的問題兩次。如果你在第一個問題上沒有得到幫助,你需要澄清它,而不是再次發佈。 – Barmar

回答

0

它看起來幾乎是正確的。問題是這樣的:當你做

foreach ($value as $rownr => $dane) { 

$rownr是從您的原始數組中的數字鍵。那麼你做

$nrwiersza = $rownr; 

因此$nrwiersza現在有該鍵的值。然後在結果數組中使用$nrwiersza作爲鍵。這意味着它必須始終與原始數組具有相同的行數,因爲$nrwiersza是唯一的。您需要使用該行中的非唯一值作爲關鍵字。因此,不要設置

$nrwiersza = $rownr; 

您可以使用此代替鍵。

$nrwiersza = $dane['personalnr']; 

這應該解決它。


foreach ($value as $rownr => $dane) { 

    // Change the key here 
    $nrwiersza = $dane['personalnr']; 

    $test[$nrwiersza]['personalnr'] = $dane['personalnr']; 
    // add the other keys from your original array 
    $test[$nrwiersza]['brutto'] = $dane['brutto']; 
    $test[$nrwiersza]['netto'] = $dane['netto']; 
    $test[$nrwiersza]["notiz"][] = $dane['notiz']; 
} 
0

只需使用personalnr爲重點和創建或添加條目,這取決於如果該號碼已經存在:

$array //your array 
$finalArray = array();  
foreach ($array["row"] AS $data){ 
    if (isset($finalArray[$data["personalnr"]])){ 
     //append notiz 
     //append to array 
     $finalArray[$data["peronalnr"]]["notiz"][] = $data["notiz"]; 
    }else{ 
     //transform notiz to an array 
     $data["notiz"] = array($data["notiz"]); 

     //Create that entry with $data["notiz"] now beeing an array. 
     $finalArray[$data["personalnr"]] = $data; 
    } 
} 

untestet,但這樣的事情。

應導致:

Array (
     [824] => Array 
      (
      [personalnr] => 824 
      [brutto] => 1000 
      [netto] => 0 
      [notiz] => Array (
       [0] => 0, 
       [1] => 1 , 
       [2] => 3 
      ) 
      ) 
     ) 
1

這是很多foreach循環,但是這也可能讓你你在找什麼,並鍵不硬編碼:

// This is the final array, so set here. 
$new = array(); 
// This is how many are in a group 
$reset = 3; 
// This is the starting number for the counter 
$i  = 1; 
// The starting number for the group(s) 
$a  = 1; 
// Loop main row 
foreach($array['row'] as $row) { 
    // Loop through each key 
    foreach($row as $key => $value) { 
     // If the array is not set, assign value 
     if(!isset($new[$a][$key])) 
      $new[$a][$key] = $value; 
     else { 
      // If the array is set already but is a string 
      if(!is_array($new[$a][$key])) { 
       // If the current value doesn't equal stored value, 
       // make an array with stored value and new value 
       if($new[$a][$key] != $value) { 
        $new[$a][$key] = array($new[$a][$key],$value); 
       } 
      } 
      // If array, make new value 
      else 
       $new[$a][$key][] = $value; 
     } 
    } 
    // Reset the increment value 
    // Also advance group number 
    if($i == $reset) { 
     $i = 0; 
     $a++; 
    } 

    $i++; 
} 
// Show new 
print_r($new); 

應該給你:

Array 
    (
     [0] =>Array 
      (
       [personalnr] => 824 
       [brutto] => 1000 
       [netto] => 0 
       [notiz] => Array 
        (
         [0] => 0 
         [1] => 1 
         [2] => 3 
        ) 
      ) 
     [1] =>Array 
      (
       [personalnr] => 822 
       [brutto] => 2000 
       [netto] => 0 
       [notiz] => Array 
        (
         [0] => 1 
         [1] => 3 
         [2] => 4 
        ) 
      ) 
    ) 
+0

腳本無法正常工作。我附上截圖。 [鏈接](https://s32.postimg.org/7r9x7mmcl/2016_06_26_23_55_12.jpg) – Jakub

+0

@Jakub我沒有得到什麼是數組鍵和值的決定性因素。根據你的例子,我擁有的是你正在尋找的東西。 – Rasclatt

+0

@Rascaltt - 謝謝你的回覆。數組鍵的決定因素總是很少的字段。我有100行。在每一行'notiz'是不同的,但其餘的字段每四行不同。所以正確的數組應該只有25行。 – Jakub

0

另外,「我肯定想知道這裏... 」可以’ t 服務器在這裏做你的重任嗎?  您所描述的內容可能會由服務器端的適當SQL查詢進行非常簡單的處理。  因此,是否有可能設計系統,以便客戶端向服務器發送適當的請求,並收到回覆後不需要進一步的JavaScript修復? 」