关于这些问题:
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]
。
答案 0 :(得分: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
)
)
)
答案 1 :(得分: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'];
}
答案 2 :(得分: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
)
)
)
答案 3 :(得分:0)
另外,“我肯定不得不在这里......”不能服务器在这里为你做重任?您所描述的内容可以通过服务器端的适当SQL查询来轻松处理。因此,是否可以设计系统以便客户端向服务器发送适当的请求,并作为响应接收不需要进一步“JavaScript重写?”的回复。