PHP Create Sub Arrays from Master Array based on keys - php

This pertains to PHP.
I have an array of arrays that is a recordset from a db query:
Array
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[3] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
[4] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[5] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[6] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
[7] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
the Value in the 7th position (6) of the sub-array represents a "group" that I want to break out into individual arrays.
So I want the above to become one array of items not in any group and then a new array for each group like this:
Array1 - All Items not in a group
Array1
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
Array2 - 1st group
Array2(
[0] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[1] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
)
Array3 - 2nd group
Array3(
[0] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[1] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
)
Im just not sure what array functions would be best used to accomplish this. I have tried getting the count of each group using the following.
I have a loop that steps through the master array (array of arrays call $rs) and while in the loop I pull out the 7th position into a variable called $HeaderRecNbr and then do this:
$GroupCount=array_count_values(array_column($rs, 6))[$HeaderRecNbr];
but Im not sure where to go from here.

Just create an array indexed by the group from index 6. This will create a sub-array for each group indexed by the group number:
foreach($array as $v) {
$groups[$v[6]][] = $v;
}
Then to get the items that are alone in a group, check if there is exactly one sub-array and add it to another array. Then remove it from the group array:
foreach($groups as $k => $v) {
if(count($v) == 1) {
$other[] = $v; // or $other[$v[0][6]][] = $v;
unset($groups[$k]);
}
}

$mainArray = ... all rows variable here..
$groupedArray = [];
$countArray = [];
foreach ($mainArray as $k => $v) {
$groupedArray[$v[6]][] = $v;
$countArray[$v[6]]++;
}
$singleItems = [];
$multipleItems = [];
foreach ($groupedArray as $k => $v) {
if ($countArray[$k] > 1) $multipleItems[] = $v;
else $singleItems = $v;
}
... do something with single and multi - groups...
1: start by grouping all items in groups by the 6th item of sub array... keep count of them at same time
2. Separate single and multiple items groups in 2 arrays...
3. Do whatever you need with multi-items groups.

This function will group them by desired index, then combine the ones that dont have any other with same index, and return an array of arrays, ungrouped, then all grouped.
function sortByIndex( $data, $index ) {
$sortedData = array();
$ungroupedData = array();
// Make sure you can loop through
if ( ! is_array( $data ) ) {
return FALSE;
}
foreach ( $data as $key => $arrayToInspect ) {
if ( ! isset( $sortedData[$arrayToInspect[$index]] ) ) {
$sortedData[$arrayToInspect[$index]] = array();
}
$sortedData[$arrayToInspect[$index]][] = $arrayToInspect;
}
// Combine as desired
foreach ( $sortedData as $groupId => $data ) {
if ( count( $data ) < 2 ) {
$ungroupedData[] = $data;
unset( $sortedData[$groupId] );
}
}
return ( array_merge( [ $ungroupedData ], array_values( $sortedData ) ) );
}

Related

Get sum of subarray column for indexed rows

I have an array of customers/people with a subarray of projects pertaining to that customer. Projects come in two types "typeJob" or "typePipeline". I need to sum the values of individual columns in the projects subarray (specifically columns 5, 6, 7 - representing revenue for month 1, month 2, and month 3) by Client.
I have tried various possible avenues including the one below, but I seem to always end up either testing if the condition is satisfied for the array as a whole rather than for each row - or if I go the way of for each loops generate errors because either there's an array/single value mismatch or the condition just doesn't do anything.
Here is what I have tried most recently. It works in the sense that it checks if "typeJob" is in the array but then sums all rows (even those without "typeJob") if "typeJob" is found for any of the Client's projects. I want to exclude rows where "typeJob" is not found.
foreach ($arrays as $row) {
$indexJob = array_search('typeJob', array_column($row['projects'],0));
if ($indexJob !== false) {
$job_total1[$row['Client']] = array_sum(array_column($row['projects'],5));
$job_total2[$row['Client']] = array_sum(array_column($row['projects'],6));
$job_total3[$row['Client']] = array_sum(array_column($row['projects'],7));
}
}
And here is the array:
(
[331] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9999
[Client] => BBC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 915
[2] => Zyxeldy
[3] =>
[4] =>
[5] => 15000
[6] =>
[7] =>
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 956
[2] => Awesome project, Step 1
[3] =>
[4] =>
[5] => 1833.3333
[6] => 1833.3333
[7] => 1833.3333
[8] =>
)
[2] => Array
(
[0] => typePipeline
[1] => 957
[2] => Awesome project, Step 2
[3] =>
[4] =>
[5] => 7000
[6] =>
[7] =>
[8] =>
)
)
)
[344] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9998
[Client] => ABC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 487
[2] => CRM integration
[3] =>
[4] =>
[5] =>
[6] => 98750
[7] => 98750
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 839
[2] => Data Warehouse
[3] =>
[4] =>
[5] =>
[6] => 11643.0601
[7] =>
[8] =>
)
)
)
[350] => Array
(
[KeyAccountID] => 1236
[KeyAccountName] => Ringo Starr
[ClientID] => 9997
[Client] => XYY
[projects] => Array
(
[0] => Array
(
[0] => typeJob
[1] => 867
[2] => Data Mining
[3] =>
[4] =>
[5] => 10000
[6] =>
[7] =>
[8] =>
)
)
)
[351] => Array
(
[KeyAccountID] => 1235
[KeyAccountName] => Poul McCartney
[ClientID] => 9996
[Client] => XYZ
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 715
[2] => XYZ, CSM
[3] =>
[4] =>
[5] => 22083.3333
[6] => 22083.3333
[7] => 22083.3333
[8] =>
)
)
)
etc.
You could filter the $row['projects'] array before summing the values:
foreach ($arrays as $row) {
$typeJobs = array_filter($row['projects'], function ($v) { return $v[0] == 'typeJob'; });
$job_total1[$row['Client']] = array_sum(array_column($typeJobs,5));
$job_total2[$row['Client']] = array_sum(array_column($typeJobs,6));
$job_total3[$row['Client']] = array_sum(array_column($typeJobs,7));
}
It may be quicker to just loop through them and keep a running total...
foreach ($arrays as $row) {
$totals = [0,0,0];
foreach ( $row['projects'] as $project ) {
if ( $project[0] == 'typeJob' ) {
$totals[0] += $project[5];
$totals[1] += $project[6];
$totals[2] += $project[7];
}
}
$job_total1[$row['Client']] = $totals[0];
$job_total2[$row['Client']] = $totals[1];
$job_total3[$row['Client']] = $totals[2];
}
it doesn't look as slim as using the array_... methods, but this only processes the array once instead of once for each call.

get values from an array depending on other value PHP

here's the result of my array
Array
(
[1] => Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] => a
)
[2] => Array
(
[0] => DEF
[1] => def
[2] => DAM
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567891
[6] =>
[7] => YES
[8] => b
)
[3] => Array
(
[0] => GHI
[1] => ghi
[2] => TEG
[3] => Street4
[4] => B
[5] => 1234567892
[6] => YES
[7] =>
[8] => c
)
[4] => Array
(
[0] => JKL
[1] => jkl
[2] => CHA
[3] => Street4
[4] => B
[5] => 1234567893
[6] => YES
[7] =>
[8] => d
)
)
I need some of values from this array by using other value for exp. if I have value of 4th index in two separate arrays.
array (
[0] => A
[1] => A
)
array (
[0] => B
[1] => B
)
Now I need to get values of 2nd index in two seperate array using above two array value
array (
[1] => rodriguez
[2] => DAM
)
array (
[1] => TEG
[2] => CHA
)
How can I do this please suggest solution.
You can try to do this way :
<?php
$firstArray = array();
foreach($bigArray as $key => $array) {
$value = $array[4]; //fetch the 4th indice
if(! isset($firstArray[$value])) {
$firstArray[$value] = array();
}
$firstArray[$value][$key] = $value;
}
$secondArray = array();
foreach($firstArray as $name => $array) {
foreach($array as $key => $info) {
$value = $bigArray[$key][2]; //fetch the 2nd indice
if(! isset($secondArray[$value])) {
$secondArray[$value] = array();
}
$secondArray[$value][$key] = $value;
}
}

how we remove index in the array and merge

Bellow is my Array in this array
****i want to remove second level index 1 and 2 and merge two arrays in one array****
$array1 = Array
(
[1] => Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
)
[2] => Array
(
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
)
[2] => Array
(
[1] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
)
[2] => Array
(
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[1] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
)
[2] => Array
(
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
)
my aspected output is bellow where second level index is removed and arrays are merged
$array1= Array
(
[1] => Array
(
[2] => 4
[3] => 2
[4] => 3
[5] =>
[9] => 4
[10] =>
[11] => 4
[12] => 4
)
[2] => Array
(
[2] => 3
[3] =>
[4] => 4.1428571428571
[5] => 5
[9] => 3
[10] => 5
[11] => 5
[12] => 3
)
)
[3] => Array
(
[2] => 5
[3] => 5
[4] =>
[5] => 4.1428571428571
[9] => 3
[10] => 4.4285714285714
[11] => 4.4285714285714
[12] => 5
)
And i tried :
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
//unset($cutvalu[$cutck]);
foreach($innervalue as $subkey => $subvalue){
#$array1[$arrkey][$subkey];
}
}
}
echo "<pre>"; print_r($array1);echo "<pre>";
Try this:
foreach($array1 as $arrkey => $arrvalue)
{
foreach($arrvalue as $innerkey => $innervalue){
foreach($innervalue as $subkey => $subvalue){
$new_array[$arrkey][$subkey] = $subvalue; // Change this line
}
}
}
echo "<pre>"; print_r($new_array);echo "<pre>";
This will give you what you're looking for.

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories