2016-06-23 37 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修复? ”