How to combine arrays from a multidimensional array and count values? - php

I'm a beginner at php and was searching for a solution all day long without success.
I have the following array:
$data = Array
(
[0] => Array
(
[my_id] => 1
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[1] => Array
(
[my_id] => 2
[my_post_id] => 123
[my_status] => 1
[my_rating] => 4
)
[2] => Array
(
[my_id] => 3
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[3] => Array
(
[my_id] => 4
[my_post_id] => 456
[my_status] => 1
[my_rating] => 5
)
[4] => Array
(
[my_id] => 5
[my_post_id] => 456
[my_status] => 1
[my_rating] => 3
)
)
and would like to merge the arrays with the same 'my_post_id' and count the values for 'my_status' and 'my_rating' which have the same 'my_post_id'.
At the end, I would like to have the following array:
$data = Array
(
[0] => Array
(
[my_post_id] => 123
[my_status] => 3
[my_rating] => 14
)
[1] => Array
(
[my_post_id] => 456
[my_status] => 2
[my_rating] => 8
)
)
I could get arrays with unique 'my_post_id' with the following code but I couldn't find out how to count the other values.
$out = array();
foreach( $data as $row ) {
$out[$row['my_post_id']] = $row;
}
$array = array_values( $out );
Any help would be much appreciated.
Daniel

This will produce the array you are looking for:
$out = array();
foreach( $data as $row ) {
if (!isset($out[$row['my_post_id']])) {
$out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
"my_status" => $row["my_status"],
"my_rating" => $row["my_rating"]);
}
else {
$out[$row['my_post_id']]["my_status"] += $row["my_status"];
$out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
}
}
results in:
Array
(
[123] => Array
(
[my_id] => 1
[my_status] => 3
[my_rating] => 14
)
[456] => Array
(
[my_id] => 4
[my_status] => 2
[my_rating] => 8
)
)

try the following - untested code
foreach($data as $key => $val){
if(!array_search($val['my_post_id'],$newArray)){
$newArray[]=array('my_post_id' => $val['my_post_id'],
'my_status' => $val['my_status'],
'my_rating' => $val['my_rating']);
}else{
$myIndex=array_search($val['my_post_id'],$newArray);
$newArray[$myIndex]['my_status']+=$val['my_status'];
$newArray[$myIndex]['my_rating']+=$val['my_rating'];
}
}

Related

combine 2 array with same key into 1 array

I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)

Create array key from value. i have created array define to bellow.i have differentiate actual result and expected result

Actual Array
Array
(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
[2] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
Expected Result
Array
(
[3] => Array(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
)
[4] => Array(
[0] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
)
You can create a new array and set value of master id as the index and put the value in it.
$data = array();
foreach($array as $key=>$value){
$data[$value['master_id']][] = $value;
}
$actualArray = array(array('sub_id' => 3, 'sub_name' => 'tttt', 'master_id' => 3),array('sub_id' => 4, 'sub_name' => 'yyyy', 'master_id' => 3),array('sub_id' => 5, 'sub_name' => 'kkkk', 'master_id' => 4));
$tempArray = array_unique(array_column($actualArray, 'master_id'));
$uniqueArray = array_intersect_key($actualArray, $tempArray);
foreach ($uniqueArray as $key => $masters) {
$count = 0;
foreach ($actualArray as $key1 => $actuals) {
if($masters['master_id'] == $actuals['master_id']){
$expectedArray[$key][$count] = $actuals;
$count++;
}
}
}

Filter a multidimensional array - filter_array (PHP)

How do you filter this multidimensional array with array_filter() based on [channel]?
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
)
[1] => Array
(
[e13268de7c56db42f8aeab2ab4c607f2] => Array
(
[dj_name] => John Doe
[show_name] => John Doe`s Trance Show
[channel] => trance
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/dummy.jpg
[time] => 11
[time_end] => 11
[mon1] => 1
[mon2] => 1
[tue2] => 1
[mon3] => 1
[fri3] => 1
[mon4] => 1
[mon5] => 1
)
)
)
Results should have only the arrays that have the value "techno", for example:
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
))
I have tried using:
$data = array_filter($dataraw, function($fs) use ($genre) {return $fs['channel'] == $genre});
EDIT:
$djs = get_posts($args);
foreach ($djs as $dj) {
$temp = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
if ($temp) $show_data[] = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
}
$datax = array_filter($show_data, function($fs) use ($genre) {
return array_values($fs)[0]['channel'] == $genre;
});
print_r($datax);
I'll assume the parse error in your code was a copy/paste mistake. The problem is that the element passed into the function is a deeper array. Try:
return current($fs)['channel'] === $genre;
Also you might want to use === so the results are as expected.

SUM values across two multidimensional arrays in PHP

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;
}

Get particular data with multiple index value with/without loop in cakephp

I have an array in which i want only lineNo and Isdirty field in each array .
My demo code is
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[ObjectType] => 12
[LineNo] => 1
[CompanyID] => 1
[BranchID] => 46
[ModifiedDate] => 2014-04-25 05:10:15
[RevisionNumber] => 6
[IsDirty] =>
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 430
[RevisionNumber] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 372
[RevisionNumber] => 1
)
)
)
Only LineNo And Isdirty field want in every index array .So please suggest me solution.
You can sue following;
$finalArr = array();
foreach ($arr as $key => $item) {
foreach ($item as $k => $v) {
$finalArr[$key][] = array(
"LineNo" => $v["LineNo"],
"IsDirty" => $v["IsDirty"]
);
}
}
Here is a working demo: Demo

Categories