Reputation: 3
In regard into these question:
https://stackoverflow.com/questions/37934938/marge-array-almost-uniques-value
I need to merge values in one array. I have startup values like these :
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
)
)
)
Problem is there row[0]
, row[1]
, row[2]
has same [personalnr]
, [brutto]
, [netto]
. [notiz]
is every row different.
Let's say that in my array I have 400 [rows]
. One row is duplicated 4 times but every time with only different [notiz]
. I need to have only 100 [rows]
but in every - need to be column [notiz]
4 times ( [notiz_1]
[notiz_2]
[notiz_3]
[notiz_4]
. Final amount of [rows]
is 100.
Array (
[row] => Array
(
[personalnr] => 824
[brutto] => 100
[netto] => 0
[notiz] => array
(
[1] = > 1
[2] = > 2
[3] = > 3
)
)
)
Like someone said I tried:
foreach ($value as $rownr => $dane) {
$nrwiersza = $rownr;
$test[$nrwiersza]['personalnr'] = $dane['personalnr'];
$test[$nrwiersza]['std'] = $dane['std'];
$test[$nrwiersza]["notiz"][] = $dane['notiz'];
}
But array $test
has still 400 rows. How to marge it with keys [personalnr]
, [brutto]
, [netto]
and put every [notiz]
into [notiz_1]
, [notiz_2]
, [notiz_3]
, [notiz_4]
.
Upvotes: 0
Views: 94
Reputation: 12505
This is a lot of foreach
loops but this may also get you what you are looking for and keys are not hardcoded:
// 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);
Should give you:
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
)
)
)
Upvotes: 1
Reputation: 8945
Also, “I sure gotta wonder here ...” can’t the server do the heavy-lifting for you here? What you are describing could be very-trivially handled by an appropriate SQL query on the server side. Therefore, might it be possible to design the system so that the client sends an appropriate request to the server, and receives in response a reply that requires no further “JavaScript mangling?”
Upvotes: 0
Reputation: 41810
It looks like it's almost right. The problem is this: when you do
foreach ($value as $rownr => $dane) {
$rownr
is the numeric key from your original array. Then you do
$nrwiersza = $rownr;
so $nrwiersza
now has the value of that key. Then you use $nrwiersza
as the key in your result array. This means that it must always have the same number of rows as your original array, because $nrwiersza
is unique. You need to use a non-unique value from the row as the key instead. So instead of setting
$nrwiersza = $rownr;
You can use this for the key instead.
$nrwiersza = $dane['personalnr'];
That should solve it.
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'];
}
Upvotes: 0
Reputation: 20889
Just use the personalnr as key and either create or append entries, depending on if the number already exists.:
$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, but something like this.
Should result in:
Array (
[824] => Array
(
[personalnr] => 824
[brutto] => 1000
[netto] => 0
[notiz] => Array (
[0] => 0,
[1] => 1 ,
[2] => 3
)
)
)
Upvotes: 0