I have this array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -5,7
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -7
[negative] => Array
(
[0] => Array
(
[amount] => -7
)
)
)
)
You can note that key 0 has +10.00 of positive and -5.7 of negative (they are money transactions).
Key 1 has +13 and -7.
Basically, I need to iterate into array and move 4.30 under key 0, taken from THE NEGATIVE of key 1.
This is must be the final array:
Array
(
[0] => Array
(
[id] => 1
[amount_positive] => 10.00
[negative_sum] => -10.00
[negative] => Array
(
[0] => Array
(
[amount] => -3.00
)
[1] => Array
(
[amount] => -2.00
)
[2] => Array
(
[amount] => -0.70
)
[3] => Array
(
[amount] => -4.30
)
)
)
[1] => Array
(
[id] => 13
[amount_positive] => 6.00
[negative_sum] => -2.70
[negative] => Array
(
[0] => Array
(
[amount] => -2.70
)
)
)
)
If you need code that performs the transformation on this particular array (where you know the array indexes), the following will do what you want:
$expense = -4.30;
$your_array[0]['negative'][] = ['amount' => $expense];
$your_array[0]['negative_sum'] += $expense;
$your_array[1]['negative'][0]['amount'] -= $expense;
$your_array[1]['negative_sum'] -= $expense;
If you want a more "general" approach for arbitrary indexes and arbitrary amount entries, you will need to rephrase your question in broader terms.
Related
I have following array which has ID as index, and some count as value. Ultimate goal is to get total of unique IDs in another array.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 25
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 24
)
)
[2] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 23
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 23
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 21
)
)
[5] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 21
)
)
[6] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 20
)
)
[7] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 20
)
)
)
I want result like below.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 48
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 44
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 44
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 41
)
)
)
I has spend few hours working on it but couldn't get solution. Can someone please help ?
Thank you.
Looks like you want to sum team object's countStats for like ids.
I've created a similar data structure, looped through it to form a temporary array that uses the id as a key, with associated amounts (to later sum - mine is amount, yours is countStats).
Then sum those and recreate desired output.
<?php
class Team
{
public $id;
public $amount;
public function __construct($id, $amount)
{
$this->id = $id;
$this->amount = $amount;
}
}
$input =
[
[new Team(1, 3)],
[new Team(1, 4)],
[new Team(2, 5)],
[new Team(2, 7)]
];
foreach($input as $subarray)
$amounts[$subarray[0]->id][]=$subarray[0]->amount;
print_r($amounts);
foreach($amounts as $k => $v)
$result[] = [new Team($k, array_sum($v))];
print_r($result);
Output:
Array
(
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 7
)
)
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[amount] => 7
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[amount] => 12
)
)
)
Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.
How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I have two arrays that have common indexes (church and office). I need to "merge" the total of the first array into the second array to get the desired output (seen below the double line). I'm not sure how to do this with array_merge(). Any help would be greatly appreciated!
Array
(
[church] => Array
(
[total] => 77
)
[office] => Array
(
[total] => 202
)
)
Array
(
[church] => Array
(
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
====================================================
Array
(
[church] => Array
(
[total] => 77
[name] => Array
(
[0] => Bill
[1] => Sally
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 25
[1] => 50
)
)
[office] => Array
(
[total] => 202
[name] => Array
(
[0] => Marta
[1] => Ruth
)
[addr] => Array
(
[0] => Address Same as Billing
[1] => Address Same as Billing
)
[message] => Array
(
[0] =>
[1] =>
)
[amount] => Array
(
[0] => 100
[1] => 100
)
)
)
Try something like this:
$a = array('church' => array('total' => 5), 'office' => array('total' => 10));
$b = array('church' => array('name' => 'church'), 'office' => array('name' => 'office'));
foreach ( $b as $key => $value ) {
$b[$key] = array_merge($a[$key], $b[$key]);
}