Replace numeric array keys with associative keys from array - php

I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)

foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine

$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);

Related

Finding matching keys in a multidimensional array and storing them in new array

I want to parse a multidimensional array and store all the matching keys into their own array.
Input Array:
array (
[Items] => Array (
[0] => AS1585.AGG
[1] => AS1585.AGG
[2] => ZBRK-1776
[3] => ZBRK-2076
[4] => ZBRK-3001
)
[Qty2] => Array (
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 6
)
[Cost] => Array (
[0] => $351.00
[1] => $351.00
[2] => $437.00
[3] => $380.00
[4] => $358.00
)
[TotalCubes2] => Array (
[0] => 168.40
[1] => 168.40
[2] => 186.05
[3] => 161.25
[4] => 140.70
)
[TotalCost2] => Array (
[0] => $1755.00
[1] => $1755.00
[2] => $2185.00
[3] => $1900.00
[4] => $2148.00
)
)
Desired output:
Array (
[Items] => AS1585.AGG
[Qty2] => 5
[Cost] => $351.00
[TotalCubes2] => 168.40
[TotalCost2] => $1755.00
)

Find and delete Duplicate in multidimensional array in php

From a User I am given a multidimensional array like this:
Array (
[classCode] => Array (
[0] => class44
[1] => class67
[2] => class34
[3] => class34
[4] => class44
)
[className] => Array (
[0] => phy
[1] => mat
[2] => chy
[3] => sci
[4] => phy
)
)
I need to find duplicate values in the arrays and also should consider '[classCode]' as primary key and should return as shown below array.
Array (
[classCode] => Array (
[0] => class44
[1] => class67
[2] => class34
)
[className] => Array (
[0] => phy
[1] => mat
[2] => chy
)
)
Use array_map:
$result = array_map(function ($items) {
return array_unique($items);
}, $your_array);

combine two arrays has same keys

I have an array like
$sele_itmid = Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 4
[4] => 4
)
$fltrd_serl_batch = Array
(
[0] => SAL121
[1] => SAL122
[2] => SAL 145
[3] => M121
[4] => M122
)
At this time i need to combine them like $sele_itmid values as keys $fltrd_serl_batch some thing like
array(
[1] => SAL121
[1] => SAL122
[1] => SAL 145
[4] => M121
[4] => M122
)
I know cant use array_combine because it wont accept duplicate keys. what is the other way to do this? Is there any array function to do this?
$itm_btch = array_map(function($key, $val) {return array($key=>$val);}, $sele_itmid, $fltrd_serl_batch);
print_r($itm_btch );
Array
(
[0] => Array
(
[1] => SAL122
)
[1] => Array
(
[1] => SAL 145
)
[2] => Array
(
[4] => M121
)
[3] => Array
(
[4] => M122
)
)

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.

How to sum values via the same key and group by other key

I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values?
My array looks like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 4
[1] => 762182Z
)
[2] => Array
(
[0] => 2
[1] => 072182X
)
[3] => Array
(
[0] => 4
[1] => 660122Y
)
[4] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[5] => Array
(
[0] => 1
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 1
[1] => 225160Y
)
[8] => Array
(
[0] => 8
[1] => 954120Y
)
[9] => Array
(
[0] => 3
[1] => 072182X
)
)
I'd like to something like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[2] => Array
(
[0] => 4
[1] => 762182Z
)
[3] => Array
(
[0] => 5
[1] => 072182X
)
[4] => Array
(
[0] => 4
[1] => 660122Y
)
[5] => Array
(
[0] => 2
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 8
[1] => 954120Y
)
)
The array in short version:
Array => TO => Array
( (
[0] => 2:790180X:26mm [0] => 2:790180X:26mm
[1] => 4:762182Z [1] => 2:790180X:24mm
[2] => 2:072182X [2] => 4:762182Z
[3] => 4:660122Y [3] => 5:072182X
[4] => 2:790180X:24mm [4] => 4:660122Y
[5] => 1:225160Y [5] => 2:225160Y
[6] => 1:244160Y [6] => 1:244160Y
[7] => 1:225160Y [7] => 8:954120Y
[8] => 8:954120Y )
[9] => 3:072182X
)
Just sum into a new array, using the various "grouping" fields as keys in the new array:
$sums = array();
foreach ($yourarray as $element) {
$sums[$element['dimension1']][$element['dimension2']]++;
}
For every dimension you need to 'group' by, you add a key to the $sums array.

Categories