Home » Php » PHP array key sum value if exist in loop with dynamic value

PHP array key sum value if exist in loop with dynamic value

Posted by: admin February 25, 2020 Leave a comment

Questions:

I create of Tree (Bill of Materials) from SQL and i’m make calculation of product. I have few if in if and if in if etc. I want to display sumamry of all individual part number with total quantitny which i’m calculating dynamicly. So, before loop and function if. i’m created three variables:

$TablicaMiH = array();
$TablicaBo = array();
$TablicaAss = array();

when I meet the conditions, which depend on the if function, each of them performs operations of adding to the array. this is:

$TablicaMiH += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];

or

$TablicaBo += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];

or

$TablicaAssy += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];

The commands execute correctly as needed, but they do not add the sum value from the $ multipler variable to me, they only do individual PartNumbers and take the last value from the $ multipler variable

here is the result of what was received from the script:

Tablica MiH:
Array ( [333/E8093] => 2 [332/F2592] => 3 [332/F3144] => 9 [332/F3147] => 21 [332/F2684] => 8 [333/D1641] => 12 [333/D1202] => 22 [332/F2588] => 1 [333/E7883] => 1 [333/E8131] => 1 )

Tablica BO:
Array ( [826/10381] => 12 [331/30854] => 7 [332/F3213] => 4 [123/06090] => 84 [1315/0307Z] => 1 [823/10874] => 1 [333/E7939] => 4 [813/10186] => 2 [332/H3476] => 3 [32/920300] => 11 [332/F3282] => 1 [32/926051] => 1 )

Tablica Ass:
Array ( [2th] => 1 [3TH] => 1 [4th] => 1 [5th] => 1 [6th] => 1 [7th] => 1 [8th] => 1 [9th] => 1 [10Th] => 1 [IN_1TH] => 1 )

and result what i need to have:

$TablicaMiH
332/F2588||5
332/F2592||10
332/F2684||9
332/F3144||27
332/F3147||38
333/D1202||40
333/D1641||16
333/E7883||1
333/E8093||12
333/E8131||1

Tablica BO:
123/06090||85
1315/0307Z||1
32/920300||11
32/926051||1
331/30854||20
332/f3213||29
332/F3282||1
332/H3476||3
333/E7939||4
813/10186||3
823/10874||1
826/10381||13

Tablica Ass:
10Th||1
1TH||1
2TH||1
3TH||1
4th||1
5th||1
6th||1
7th||1
8th||1
9th||1
IN_1TH||1

I Hope you understand me what i mean, and you can help me, thank you

Example to understand my problem:

<?php

$exampleArraY = array(
            "PartNumber1" => 1,
            "PartNumber2" => 1,
            "PartNumber3" => 1,
            "PartNumber4" => 1,
            "PartNumber5" => 1,
            "PartNumber6" => 1,
            );

$value = "PartNumber1";
$value2 = "PartNumber2";
$value3 = "PartNumber3";
$value4 = "PartNumber4";
$value5 = "PartNumber5";
$value6 = "PartNumber6";
$value7 = "PartNumber7";

$multipler = 1;

if(in_array($value, $exampleArraY)){
    $exampleArraY[$value][1] += $multipler;
}else {
    $exampleArraY += [$value => $multipler];
}
if(in_array($value2, $exampleArraY)){
    $exampleArraY[$value2][1] += $multipler;
}else {
    $exampleArraY += [$value2 => $multipler];
}
if(in_array($value3, $exampleArraY)){
    $exampleArraY[$value3][1] += $multipler;
}else {
    $exampleArraY += [$value3 => $multipler];
}
if(in_array($value4, $exampleArraY)){
    $exampleArraY[$value4][1] += $multipler;
}else {
    $exampleArraY += [$value4 => $multipler];
}
if(in_array($value5, $exampleArraY)){
    $exampleArraY[$value5][1] += $multipler;
}else {
    $exampleArraY += [$value5 => $multipler];
}
if(in_array($value6, $exampleArraY)){
    $exampleArraY[$value6][1] += $multipler;
}else {
    $exampleArraY += [$value6 => $multipler];
}
if(in_array($value7, $exampleArraY)){
    $exampleArraY[$value7][1] += $multipler;
}else {
    $exampleArraY += [$value7 => $multipler];
}

print_r($exampleArraY);

?>

Result:

Array ( [PartNumber1] => 1 [PartNumber2] => 1 [PartNumber3] => 1 [PartNumber4] => 1 [PartNumber5] => 1 [PartNumber6] => 1 )

Desired Result:

Array ( [PartNumber1] => 2 [PartNumber2] => 2 [PartNumber3] => 2 [PartNumber4] => 2 [PartNumber5] => 2 [PartNumber6] => 2  [PartNumber7] => 1 )
How to&Answers:

If I understand the problem correctly, you might be looking for something like this:

    // create the starting array 
    $TablicaMiH = [];

    // ... do interesting things... in a loop most likely....

    // if we dont have a value for this key yet, set it to 0
    if (!isset($TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']])) {
        $TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] = 0;
    }
    // now add the multiplier for this part number
    $TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] += $multipler;

Answer:

The problem like i explained was in construction of arrays. Here is my solution to your problem:

<?php
$exampleArraY = array(
            "PartNumber1" => 1,
            "PartNumber2" => 1,
            "PartNumber3" => 1,
            "PartNumber4" => 1,
            "PartNumber5" => 1,
            "PartNumber6" => 1,
            );

$values = array(
    "value" => "PartNumber1",
    "value2" => "PartNumber2",
    "value3" => "PartNumber3",
    "value4" => "PartNumber4",
    "value5" => "PartNumber5",
    "value6" => "PartNumber6",
    "value7" => "PartNumber7");

$multipler = 1;

foreach($values as $key => $item){
    if(isset($exampleArraY[$item])){
        echo $exampleArraY[$item] += $multipler;
    }else {
        $exampleArraY[$item] = $multipler;
    }

}

Output:

array(7) {
  ["PartNumber1"]=>int(2)
  ["PartNumber2"]=>int(2)
  ["PartNumber3"]=>int(2)
  ["PartNumber4"]=>int(2)
  ["PartNumber5"]=>int(2)
  ["PartNumber6"]=>int(2)
  ["PartNumber7"]=>int(1)
}