I'm trying to build a multidimentional array of matches found within a foreach loop. After one loop the array is correct but on the second loop, the earlier array values are overwritten. What is going on?
$matches = array();
foreach ($promotions as $promotion) {
$matches = array();
foreach ($saleitems as $saleitem) {
if ($saleitem['PROMO_CODE'] == $promotion['SALES_CODE']) {
$matches[] = array('ID'=>$saleitem['ID'], "LINENO"=>$saleitem['LINE'], "SAVING"=>"0", 'SALEINC'=>$saleitem['SALEINC']);
}
}
//other code with works out discount etc.
$linesarray[] = array("CODE"=>$promotion['CODE'], "LINES"=>$matches);
print_r($linesarray);
echo "<p>";
}
Outputs this:
Array ( [0] => Array ( [CODE] => 5 [LINES] => Array ( [0] => Array ([ID] => 51016 [LINENO] => 4 [SAVING] => 5 [SALEINC] => 15.00 ) [1] => Array ([ID] => 51013 [LINENO] => 3 [SAVING] => 5 [SALEINC] => 15.00 ) ) ) )
Array ( [0] => Array ( [CODE] => 5 [LINES] => Array ( [0] => Array ( [ID] => 51016 [LINENO] => 4 [SAVING] => 5 [SALEINC] => 15.00 ) [1] => Array ([ID] => 43930 [LINENO] => 2 [SAVING] => 0 [SALEINC] => 16.00 ) ) ) [1] => Array ( [CODE] => 7 [LINES] => Array ( [0] => Array ([ID] => 43914 [LINENO] => 1 [SAVING] => 6 [SALEINC] => 16.00 ) [1] => Array ([ID] => 43930 [LINENO] => 2 [SAVING] => 6 [SALEINC] => 16.00 ) ) ) )
As you can see LINENO 3 has been replaced the first array on second loop. Why?
The $matches = array(); inside the loop will reinitialize the array. The $matches = array(); before the loop is fine.
$matches[] = array('ID'=>$saleitem['ID'], "LINENO"=>$saleitem['LINE'], "SAVING"=>"0", 'SALEINC'=>$saleitem['SALEINC']);
I see you fixed "SAVING"=>"0" but your outputs have other result [SAVING] => 5 Did you run your code again?
You should give us $promotions and $saleitems array.
Related
This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)
I have a problem how to SUM values of two multidimensional arrays in PHP.
Two arrays are storred in variables for example $array_1 and $array_2
Both $array_1 and $array_2 have sub-arrays, which again have data stored in their respective arrays. Notice that data is organized as x-data and y-data, where y-data is just a DATE stamp.
I need to SUM x-data values of these two arrays $array_1 and $array_2, with respect to y-data (DATE) dimension. This is why it got tricky for me, the closes answers I have found have only numbers.
Example:
$array_1
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 2
[data2] => 4
[data3] => 6
[data4] => 8
[data5] => 10
[data6] => 12
[data7] => 14
[data8] => 16
)
[y-data] => Array
(
[date] => 20141128
)
)
)
$array_2
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141128
)
)
)
$result = $array_1 + $array_2 should look like this:
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 2
[data3] => 4
[data4] => 6
[data5] => 8
[data6] => 10
[data7] => 12
[data8] => 14
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 2
[data2] => 5
[data3] => 8
[data4] => 11
[data5] => 14
[data6] => 17
[data7] => 20
[data8] => 23
)
[y-data] => Array
(
[date] => 20141128
)
)
)
I have tried some things, like foreach() inside foreach() but I got a multiplication effect of the array values (2 x 2 = 4 subarrays instead of 2).
Any help?
Thanks!
Here's a partial answer as this should only only work if $array1 and $array2 have same number of elements and have unique y-datas, and each of their x-data (with y-data in common) have the same number of elements.
$result = array_multisum($array1, $array2);
function array_multisum($array1, $array2)
{
$newArray = array();
foreach ($array1 as $object1) {
$newObject = new stdClass();
// find object in the 2nd array having the same data as current object
foreach ($array2 as $object2) {
// if object is found, sum the x-data with the current object
if ($object1->{'y-data'}['date'] === $object2->{'y-data'}['date']) {
$newObject->{'x-data'} = data_sum($object1->{'x-data'}, $object2->{'x-data'});
break;
}
}
$newObject->{'y-data'} = $object1->{'y-data'};
$newArray[] = $newObject;
}
return $newArray;
}
function data_sum($data1, $data2)
{
$newData = array();
// sum up the values for each key
foreach (array_keys($data1) as $key) {
$newData[$key] = $data1[$key] + $data2[$key];
}
return $newData;
}
I have a Array like this
Array
(
[0] => Array
(
[vendor_id] => 2
[vendor_total_order] => 80
)
[1] => Array
(
[vendor_id] => 2
[vendor_total_order] => 100
)
[2] => Array
(
[vendor_id] => 1
[vendor_total_order] => 150
)
[3] => Array
(
[vendor_id] => 3
[vendor_total_order] => 80
)
[4] => Array
(
[vendor_id] => 5
[vendor_total_order] => 150
)
[5] => Array
(
[vendor_id] => 1
[vendor_total_order] => 110
)
)
I want to simplify this array in such a way that if the 'vendor_id' are same for two values there there accumulated/summed value should be assigned to 'vendor_total_order' to that 'vendor_id'(basically we are removing values which are having same vendor_id with the total value of duplicates).
So when i provide the above array as input the output should look like as follow
Array
(
[0] => Array
(
[vendor_id] => 2
[vendor_total_order] => 180
)
[1] => Array
(
[vendor_id] => 1
[vendor_total_order] => 260
)
[2] => Array
(
[vendor_id] => 3
[vendor_total_order] => 80
)
[3] => Array
(
[vendor_id] => 5
[vendor_total_order] => 150
)
)
How can i do this ?
You just need to group them using a foreach. Example:
$total = array();
foreach ($array as $key => $value) {
if(!isset($total[$value['vendor_id']])) {
$total[$value['vendor_id']] = array('vendor_id' => $value['vendor_id'], 'vendor_total_order' => 0);
}
$total[$value['vendor_id']]['vendor_total_order'] += $value['vendor_total_order'];
}
$total = array_values($total); // simple reindex
echo '<pre>';
print_r($total);
Sample Output
Your best bet is to write a custom script that does the following:
Creates an empty new "result" array
Iterates over the current array and for each item:
If the item doesn't exist in result, insert it, otherwise update the total_value value as the sum of it's current value and the new item.
Save the result array
I have a php array that just like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[1] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[2] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
and I want to combine the duplicate values, add "qnty" index value and print that array like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] =>2
)
[1] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
How can i combine this array. Please help me
try the code below. I am assuming the your array name is $products
$merged = array();
foreach($products as $product) {
$key = $product['product'];
if(!array_key_exists($key, $merged)) {
$merged[$key] = $product;
}
else {
$merged[$key]['qnty'] += $product['qnty'];
}
}
echo '<pre>';
print_r($merged);
exit;
and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.