0

I've a following Multidimensional array named $data

$data =
Array
(
[0] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 1000
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 1000
        [ApprovedActivities] => 0
        [AvailableBalance] => 1000
        [TotalApprovedClaims] => 0
        [Balance] => 1000
        [TotalUnApprovedClaims] => 0
        [Exposure] => 1000
    )

[1] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 50
        [StartOfAllocation] => 50
        [ApprovedActivities] => 0
        [AvailableBalance] => 50
        [TotalApprovedClaims] => 0
        [Balance] => 50
        [TotalUnApprovedClaims] => 0
        [Exposure] => 50
    )

[2] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => EMEA
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 25
        [StartOfAllocation] => 25
        [ApprovedActivities] => 0
        [AvailableBalance] => 25
        [TotalApprovedClaims] => 0
        [Balance] => 25
        [TotalUnApprovedClaims] => 0
        [Exposure] => 25
    )

[3] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => APJeC
        [SubRegion] => India
        [Country] => India
        [typeofrec] => 0
        [TotalAllocations] => 500
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 500
        [ApprovedActivities] => 0
        [AvailableBalance] => 500
        [TotalApprovedClaims] => 0
        [Balance] => 500
        [TotalUnApprovedClaims] => 0
        [Exposure] => 500
    )

)

and

$amount =10;
$index = 2  // selected array index for change

The above information i have and i need to update the array with new [AvailableBalance] based on below calculations.

Next i need to get the [AvailableBalance] from the array $index (index 2 )

$existing_available = 25;

Subtract the $amount from $existing_availablet

$remaining_amount = 15; // where ($existing_available - $amount)

Update the [AvailableBalance] = 15($remaining_amount) using array $index (index2).

so my final array look like below:

$data =
Array
(
[0] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 1000
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 1000
        [ApprovedActivities] => 0
        [AvailableBalance] => 1000
        [TotalApprovedClaims] => 0
        [Balance] => 1000
        [TotalUnApprovedClaims] => 0
        [Exposure] => 1000
    )

[1] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 50
        [StartOfAllocation] => 50
        [ApprovedActivities] => 0
        [AvailableBalance] => 50
        [TotalApprovedClaims] => 0
        [Balance] => 50
        [TotalUnApprovedClaims] => 0
        [Exposure] => 50
    )

[2] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => EMEA
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 25
        [StartOfAllocation] => 25
        [ApprovedActivities] => 0
        [AvailableBalance] => 15      // updated value 
        [TotalApprovedClaims] => 0
        [Balance] => 25
        [TotalUnApprovedClaims] => 0
        [Exposure] => 25
    )

[3] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => APJeC
        [SubRegion] => India
        [Country] => India
        [typeofrec] => 0
        [TotalAllocations] => 500
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 500
        [ApprovedActivities] => 0
        [AvailableBalance] => 500
        [TotalApprovedClaims] => 0
        [Balance] => 500
        [TotalUnApprovedClaims] => 0
        [Exposure] => 500
    )

)

Please help me to try this issue.

I am new in php and stack overflow.

Thanks in advance..

3 Answers 3

2

There is solution:

$index = 2;
$amount = 10;

$existing_available = $data[$index]['AvailableBalance']; //25

$remaining_amount = $existing_available - $amount; //15

//update AvailableBalance of 2nd array
$data[$index]['AvailableBalance'] = $remaining_amount;

Working example (other fields are ommitted for simplicity): CLICK

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry but this will not work. It will append new value under index 2.
It works ok, I added working example, just click "Execute code" button to see result.
Oh yes.Previously it was not working. I think i had missed something. Good luck. +1.
Instead of declaring a new variable you can update it with a same array values.
0

If you calculate the array index and amount($index, $amount) then update the values directly to the array $data,

$index = 2;
$amount = 10;     
$data[$index]['AvailableBalance'] = $data[$index]['AvailableBalance'] - $amount;

Comments

0

Try this :

$index=2;
$amount=10;
foreach($result as $key => $subarray) {
   if($key == $index) { //change this key according to your requirements.
      foreach($subarray as $subkey => $subsubarray) {
         if($subkey == "AvailableBalance") {
             $result[$key][$subkey] = $subsubarray-$amount;
         }
      }
   }
}

Demo : https://eval.in/520720

8 Comments

Sorry this is not working.It will append new value for all under index 2
have you seen my demo? And also check my updated answer.
Thanks Mr.Engineer. it's Working Perfectly.
Why do you want to loop? while you can update the array values values directly based on the index.
@aslawin OP didn't write that $index and $amount are values based on calculations. Please read it carefully. OP have this information (just for example) and want to update an array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.