Compare Array elements and add based on key and value - php

I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??

Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records

This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z

Related

How do you inner join / merge two time series data arrays in PHP?

I want to join/combine two time series arrays into one single array
where there are values for the same time stamp only.
Each time series array looks like this
$ARRAY1 =
Array
(
[1] => Array
(
[0] => 1570572000
[1] => 1570485600
[2] => 1570399200
[3] => 1570140000
[4] => 1570053600
[5] => 1569967200
[6] => 1569880800
[7] => 1569794400
[8] => 1569535200
[9] => 1569448800
[10] => 1569362400
[11] => 1569276000
[12] => 1569189600
[13] => 1568930400
[14] => 1568844000
[15] => 1568757600
)
[2] => Array
(
[0] => 0.7967851
[1] => -1.5081206
[2] => -0.4314591
[3] => 1.3359961
[4] => 0.8296883
[5] => -1.7323694
[6] => -1.1625164
[7] => 0.4908599
[8] => -0.5353535
[9] => -0.2083193
[10] => 0.5880961
and i have a similar one just with another set of data in the column and possibly different timestamps as well.
In MYSQL i would just do an inner join. But in PHP if i had these two arrays $ARRAY1 and $ARRAY2 how do i merge them in to an array with
three columns (column1 = timestamps, column2 = data1, column3 = data2)
where i only include timestamps where both data1 and data2 are present?
array_merge or array_combine or array_intersect are not doing this job.
and i can only think of a crude method through a for loop and doing If timestamp1 = timestamp2 then update new array with data1 and data2.
but surely there must be a more elegant way to solve this?

PHP - Converting Array to group duplicates together - phpspreadsheet

I have a csv uploader i'm creating to push to an order api. Using phpSpreadsheets toArray method, i have an array like so:
Array
(
[0] => Array
(
[0] => product_sku
[1] => product_qty
[2] => shipping_name
[3] => shipping_address1
[4] => shipping_address2
[5] => shipping_city
[6] => shipping_county
[7] => shipping_postcode
[8] => shipping_type
[9] => customer_id
)
[1] => Array
(
[0] => test_sku_1
[1] => 3
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[2] => Array
(
[0] => test_sku_2
[1] => 2
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[3] => Array
(
[0] => test_sku_3
[1] => 7
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => L17MA2
[8] => 1
[9] => 14994333
)
)
Each order will be on a new line, however if a customer orders two different items, i need to group them together using their postcode. As such i was aiming to rearange into this format:
[orders] => Array(
Array(
[shipping_name] => "Bruce wayne",
[customer_id] => 14994333,
[address] => Array(
[shipping_address1] => "The Manor",
[shipping_address2] => "Near Arham Asylumn",
[shipping_city] => "Gotham",
[shipping_county] => "Greater Gotham",
[shipping_postcode] => "B17MAN",
)
[products] => Array(
Array(
[sku] => "test_sku_1",
[quantity] => 3
),
Array(
[sku] => "test_sku_2",
[quantity] => "2"
)
)
)
)
Once of the first problems i encountered was trying to match the postcodes. I managed to get a count using:
$getDuplicates = array_count_values(array_map(function($duplicates) {
return $duplicates[7]; //Where 7 is the postcode
}, $rows));
This worked in counting what i needed correctly. However from there i'm hitting a brick wall. If i'm counting the duplicates, i need it to also make a note of the rows it's already gone through so they aren't pushed incorrectly into the new array i want to make.
Pseudo it should be:
for each rows as row{
if row isn't set to be ignored{
for each count of this postcode{
array_push the product sku and qty
mark these rows as rows to be ignored
}
}
}
Can anyone help me with this?
So, long story short, i've been so caught up in this i completely missed the obvious. To solve this, i took the original array from phpspreadsheet and did an array_multisort:
foreach ($rows as $key => $row) {
$postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;
I then just worked my way through the array and if the postcode changed, i'd create a new array, otherwise i'd just push straight into the correct array for the products.
I feel really stupid i didn't think of this before. Thank you to #apokryfos for trying to help!

How to count number Array which inside of another Array

Lets say I have an Array like this:
print_r($request_params);
Array (
[chat_id] => 120613381
[text] => Array (
[0] => saadat
[1] => saadat
[2] => saadat
[3] => saadat
[4] => saadat
[5] => saadat
[6] => donya
[7] => donya
[8] => donya
[9]
)
)
As you can see inside of [text] I have multiple values. Now I want to count that but I don't know how to access that part.
My try was this but it returns 0:
echo $num3 = count($request_params[2]["text"]);
So what is your suggest..
You don't need [2]:
echo count($request_params['text']);
I think you are looking for array_count_values.
It will return an associative array with the count of each name.
$counts = array_count_values($request_params['text']);
Var_dump($counts);
This should return for example:
[saadat] => 6
[donya] => 3

Reorganise array, move indexes to specific locations php

I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.

Re-order PHP array by middle key as start (Circular Sorting)

very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.

Categories