array_diff with calculation - php

Please, consider the following arrays:
$reference = array(
'080604' => 4,
'080703' => 4,
'080734' => 2,
'080819' => 2,
'088341' => 2,
'805238' => 20,
'805283' => 4,
'805290' => 2,
'805849' => 2,
'806051' => 2,
'806068' => 2,
);
$test = array(
'080604' => 2,
'080703' => 4,
'080819' => 1,
'088341' => 2,
'805238' => 20,
'805283' => 4,
'805290' => 2,
'805849' => 2,
'806051' => 2,
'806068' => 2,
);
They are quite similar, but can have some various differences, e.g. it's possible that:
- some keys of $reference are not present in $test at all
- some keys of $test are not present in $reference at all
- all keys are present, but the values in $reference and $test are different (sometimes $reference value is bigger than $test and sometimes the value of $test is bigger than $reference)
I need to find out the differences automatically and to output them in a way, that not only the difference in count itself, but also a description is provided, e.g.
$result = [
'080604' => [
'reference' => 4,
'test' => 2
]
];
If some value is in only one of the lists:
$result = [
'1234567890' => [
'reference' => 0,
'test' => 2
]
];
or something like that.
Does someone have an idea, which is the best way to accomplish this in an elegant way? Thank you very much!

Iterate over each and populate the array with values if present:
$combined = [];
foreach ($reference as $key => $val) {
$combined[$key] = [
'test' => 0,
'reference' => $val,
];
}
foreach ($test as $key => $val) {
if (!isset($combined[$key])) {
$combined[$key] = [
'reference' => 0,
'test' => 0,
]
}
$combined[$key]['test'] = $val;
}
$combined will contain both values from both arrays with reference to both the elements from $reference and $test.

try
$result = array_diff($reference, $test);
print_r($result)

Related

Group rows in a multidimensional array and sum the "count" elements in each group [duplicate]

This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 7 months ago.
I have an array of associative arrays and I would like to group them by the rows by the values for A, B, and C and sum the count value for each group.
$array = [
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'test1', 'C' => 2, 'count' => 1],
];
I need a result like this:
[
["A" => "O", "B" => "O", "C" => 1, "count" => 2],
["A" => "Test", "B" => "Test", "C" => 1, "count" => 2],
["A" => "Test", "B" => "test1", "C" => 2, "count" => 1]
]
In order to make this you need to loop through the array and check where the attributes "A", "B", "C" are equal. I tried doing this, but I couldn't fix it.
$countedArray[0] = $array[0];
foreach ($array as $item) {
$occKey = array_filter(
$countedArray,
function ($countedItem, $key) use ($array) {
if ($countedItem['A'] == $item['A']
&& $countedItem['B'] == $item['B']
&& $countedItem['C'] == $item['C']
) {
$countedItem[$key]['count'] = countedItem[$key]['count'] + 1
} else {
array_push(
$countedArray,
[
'A' => $item['A'],
'B' => $item['B'],
'C' => $item['C'],
'count' => 1
]
);
}
},
ARRAY_FILTER_USE_BOTH
);
}
I've done my best to make it less verbose. I welcome any suggestions. Here's my proposed solution:
function sumOccurrences(array $original): array
{
$summed = [];
foreach ($original as $value) {
// here we get the array without the 'count' key - everything we need to compare
$comparisonElement = array_filter($value, function ($key) {
return $key !== 'count';
}, ARRAY_FILTER_USE_KEY);
// we search with strict comparison (third param - true) - see reasoning below
$foundAt = array_search($comparisonElement, array_column($summed, 'element'), true);
if ($foundAt === false) {
// we separate the values we compare and the count for easier handling
$summed[] = ['element' => $comparisonElement, 'count' => $value['count']];
} else {
// if we've run into an existing element, just increase the count
$summed[$foundAt]['count'] += $value['count'];
}
}
// since we separated count from the values for comparison, we have to merge them now
return array_map(function ($a) {
// $a['count'] is wrapped in an array as it's just an integer
return array_merge($a['element'], ['count' => $a['count']]);
}, $summed);
}
In order to make it less verbose, I've opted to compare arrays directly. Other than being less verbose, another benefit is that this will work if additional key => value pairs are introduced to the array without any addition to logic. Everything that is not count gets compared, no matter how many pairs exist. It will also cover any nested arrays (for example 'C' => ['D' => 1]).
But, this comes at a price - we must use strict comparison because loose can give undesired results (for example, ['a'] == [0] will return true). Strict comparison also means that it won't work if any values are objects (strict comparison means it's checking for the same instance) and that arrays will only be matched if they have the same key => value pairs in the same order. This solution assumes that your array (and any nested ones) are already sorted.
If that is not the case, we'd have to sort it before comparing. Normally, ksort would do the job, but to support nested arrays, we'd have to devise a recursive sort by key:
function ksortRecursive(array &$array): void
{
ksort($array);
foreach ($array as &$value) {
if (is_array($value)) {
ksortRecursive($value);
}
}
}
and call it before we do array_search.
Now if we assume a starting array like in your example, the following should give you the desired result:
$original = [
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'test1', 'C' => 2, 'count' => 1],
];
var_dump(sumOccurrences($original));
The accepted answer is working waaaaay too hard for what is a basic task.
You merely need to use temporary, composite keys (based on the first three elements' values) to form grouped results. When a new row matches a pre-existing group, simply add its count to the stored count for the group. When the loop finishes, call array_values() to re-index the first level of the result array.
Code: (Demo)
$array = [
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'O', 'B' => 'O', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'Test', 'C' => 1, 'count' => 1],
['A' => 'Test', 'B' => 'test1', 'C' => 2, 'count' => 1],
];
$result = [];
foreach ($array as $row) {
$key = implode('~', array_slice($row, 0, 3));
if (!isset($result[$key])) {
$result[$key] = $row;
} else {
$result[$key]['count'] += $row['count'];
}
}
var_export(array_values($result));

Reformat array without temp variable

I have the next array
[
['id' => 30, 'count' => 3],
['id' => 45, 'count' => 7]
]
I need it to be
[
30 => ['count' => 3],
45 => ['count' => 7]
]
What I did
$formatted = [];
foreach ($services as $service) {
$formatted[$service['id']] = [
'count' => $service['count']
];
}
What I'd like is a more elegant solution without the temporary $formatted variable. Thanks!
Update. Thanks a lot #rtrigoso !
With the laravel collection, my code looks next
$services->reduce(function ($carry, $item) {
$carry[$item['id']] = ['count' => $item['count']];
return $carry;
});
You can do this in one line with array_column:
$array = array_column($array, null, 'id');
The one difference between your desired output is that this will still contain the id key in the second level of the array, like so:
[
30 => ['id' => 30, 'count' => 3],
45 => ['id' => 45, 'count' => 7],
]
but that hopefully shouldn't cause any problems. If you do need to remove it, you can do it with something like:
$array = array_map(function ($e) {
unset($e['id']);
return $e;
}, $array);
This approach is probably best if your rows could potentially have a lot more keys in them in future, i.e. it's quicker to list the keys to remove rather than the ones to keep. If not, and you'll only have a count, then to be honest your original example is probably the best you'll get.
You can use array_reduce
$x_arr = array(
array('id' => 30, 'count' => 3),
array('id' => 45, 'count' => 7),
);
$y_arr = array_reduce($x_arr, function ($result, $item) {
$result[$item['id']] = array('count' => $item['count']);
return $result;
}, array());
print_r($y_arr);
It will give you your desired result:
Array
(
[30] => Array
(
[count] => 3
)
[45] => Array
(
[count] => 7
)
)

How can I add a $key=>$value pair to an associative array using a foreach loop in PHP?

I have two associative arrays I wish to combine with a foreach loop:
$arr1 = array( 'wikipedia.org' => 11, 'bing.com' => 9, 'google.com' => 8, 'blekko.com' => 7, 'groove.com' => 6, 'blo.com' => 5, 'ekko.com' => 4, 'rokko.com' => 3, 'always.com' => 2, 'popo.com' => 1);
$arr2 = array( 'google.com' => 20, 'blekko.com' => 19, 'wikipedia.org' => 8, 'bing.com' => 7, 'blo.com' => 6, 'ekko.com' => 5, 'groove.com' => 4, 'popo.com' => 3, 'always.com' => 2, 'rokko.com' => 1);
I use a new array
$combined = $arr1;
with a foreach loop
foreach($arr2 as $key=>$value)
{
array_push($combined,$value);
}
... which adds the value but not the key. I think I know why, but cannot find a way to add the key and the value. This works for a single line, but frustratingly nor in a foreach loop!
$combined=array_merge(array('blovk.com'=>'44'),$combined);
$aggregatedResults[$key] = $value;
It should be that simple...

Simplify foreach array builder with isset tests for keys

For the following dataset ($data), I'm building a multidimensional array. For each unique key (a) there will be an array of b values associated with it.
$data = [
['a' => 1, 'b' => 23],
['a' => 1, 'b' => 97],
['a' => 2, 'b' => 23],
['a' => 1, 'b' => 47],
['a' => 3, 'b' => 23],
];
$result = [];
foreach ($data as $d) {
if (!isset($result[$d['a']]))
$result[$d['a']] = [];
$result[$d['a']][] = $d['b'];
}
print_r($result);
/*
Output should be:
[
1 => [23, 97, 47],
2 => [23],
3 => [23],
]
*/
The if (!isset($result[$d['a']])) $result[$d['a']] = []; part is ugly. Is there a sexier/more-efficient way to build this desired output?
Update:
Thanks everyone, looks like I could just go (without even initializing $result = [];)
foreach ($data as $d)
$result[$d['a']][] = $d['b'];
That check is not needed at all. If it is not a array, it will created when the first item is added.
So the "sexier" solution is to remove the if and initialization...
Dont have to use the isset() function.
try this code to make it "sexier".
$data = [
['a' => 1, 'b' => 23],
['a' => 1, 'b' => 97],
['a' => 2, 'b' => 23],
['a' => 1, 'b' => 47],
['a' => 3, 'b' => 23],
];
$result = [];
foreach ($data as $d) {
$result[$d['a']][]= $d['b'];
}
print_r($result);

In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays? [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed last month.
I'm sure this question has been asked before, my apologies for not finding it first.
The original array:
[0] => Array
(
[categoryId] => 1
[eventId] => 2
[eventName] => 3
[vendorName] => 4
)
[1] => Array
(
[categoryId] => 5
[eventId] => 6
[eventName] => 7
[vendorName] => 8
)
[2] => Array
(
[categoryId] => 9
[eventId] => 10
[eventName] => 11
[vendorName] => 12
)
My hoped for result out of: print_r(get_values_from_a_key_in_arrays('categoryId', $array));
[0] => 1
[1] => 5
[2] => 9
I'm just looking for something cleaner than writing my own foreach based function. If foreach is the answer, I already have that in place.
Edit: I don't want to use a hard-coded key, I was just showing an example call to the solution. Thanks! ^_^
Quick Grab Solution for PHP 5.3:
private function pluck($key, $data) {
return array_reduce($data, function($result, $array) use($key) {
isset($array[$key]) && $result[] = $array[$key];
return $result;
}, array());
}
So, the cool thing about higher-order collection/iterator functions such as pluck, filter, each, map, and friends is that they can be mixed and matched to compose a more complex set of operations.
Most languages provide these types of functions (look for packages like collection, iterator, or enumeration/enumerable)...some provide more functions than others and you will commonly see that the functions are named differently across languages (i.e. collect == map, reduce == fold). If a function doesn't exist in your language, you can create it from the ones that do exist.
As for your test case...we can use array_reduce to implement pluck. The first version I posted relied on array_map; however, I agree with #salathe that array_reduce is more succinct for this task; array_map is an OK option, but you end up having to do more work in the end. array_reduce can look a bit odd at first, but if the callback is neatly organized, all is well.
A less naive pluck would also check to see if it can "call" (a function/method) on the iterated value. In the naive implementation below, we assume the structure to be a hash (associative array).
This will setup the test-case data (Fixtures):
<?php
$data[] = array('categoryId' => 1, 'eventId' => 2, 'eventName' => 3, 'vendorName' => 4);
$data[] = array('categoryId' => 5, 'eventId' => 6, 'eventName' => 7, 'vendorName' => 8);
$data[] = array('categoryId' => 9, 'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
$data[] = array(/* no categoryId */ 'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
$data[] = array('categoryId' => false,'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
$data[] = array('categoryId' => 0.0, 'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
Choose the version of pluck you'd prefer
$preferredPluck = 'pluck_array_reduce'; // or pluck_array_map
"pluck" for PHP 5.3+: array_reduce provides a terse implementation though not as easy to reason about as the array_map version:
function pluck_array_reduce($key, $data) {
return array_reduce($data, function($result, $array) use($key){
isset($array[$key]) &&
$result[] = $array[$key];
return $result;
}, array());
}
"pluck" for PHP 5.3+: array_map isn't perfect for this so we have to do more checking (and it still doesn't account for many potential cases):
function pluck_array_map($key, $data) {
$map = array_map(function($array) use($key){
return isset($array[$key]) ? $array[$key] : null;
}, $data);
// is_scalar isn't perfect; to make this right for you, you may have to adjust
return array_filter($map, 'is_scalar');
}
"pluck" for legacy PHP <5.3
We could have used the legacy create_function; however, it is bad form, not recommended, and also not at all elegant, thus, I've decided not to show it.
function pluck_compat($key, $data) {
$map = array();
foreach ($data as $array) {
if (array_key_exists($key, $array)) {
$map[] = $array[$key];
}
}
unset($array);
return $map;
}
Here we choose a version of "pluck" to call based on the version of PHP we are running. If you run the entire script, you should get the correct answer no matter what version you are on.
$actual = version_compare(PHP_VERSION, '5.3.0', '>=')
? $preferredPluck('categoryId', $data)
: pluck_compat('categoryId', $data);
$expected = array(1, 5, 9, false, 0.0);
$variance = count(array_diff($expected, $actual));
var_dump($expected, $actual);
echo PHP_EOL;
echo 'variance: ', $variance, PHP_EOL;
print #assert($variance)
? 'Assertion Failed'
: 'Assertion Passed';
Notice there is no ending '?>'. That is because it isn't needed. More good can come of leaving it off than from keeping it around.
FWIW, it looks like this is being added to PHP 5.5 as array_column.
Mapping is what you need:
$input = array(
array(
'categoryId' => 1,
'eventId' => 2,
'eventName' => 3,
'vendorName' => 4,
),
array(
'categoryId' => 5,
'eventId' => 6,
'eventName' => 7,
'vendorName' => 8,
),
array(
'categoryId' => 9,
'eventId' => 10,
'eventName' => 11,
'vendorName' => 12,
),
);
$result = array_map(function($val){
return $val['categoryId'];
}, $input);
Or creating a function you wanted:
function get_values_from_a_key_in_arrays($key, $input){
return array_map(function($val) use ($key) {
return $val[$key];
}, $input);
};
and then using it:
$result = get_values_from_a_key_in_arrays('categoryId', $array);
It will work in PHP >= 5.3, where anonymous callbacks are allowed. For earlier versions you will need to define callback earlier and pass its name instead of anonymous function.
There's no built-in function for this, but it's usually referred as "pluck".
<?php
$a = array(
array('a' => 1, 'b' => 2),
array('a' => 2, 'b' => 2),
array('a' => 3, 'b' => 2),
array('a' => 4, 'b' => 2)
);
function get_a($v) {
return $v['a'];
}
var_dump(array_map('get_a', $a));
You can use an create_function or an anonymous function (PHP 5.3 >=)
<?php
$a = array(
array('a' => 1, 'b' => 2),
array('a' => 2, 'b' => 2),
array('a' => 3, 'b' => 2),
array('a' => 4, 'b' => 2)
);
var_dump(array_map(create_function('$v', 'return $v["a"];'), $a));
I'd write a callback function, as above, and then use it with array_map.
As of PHP 5.5, use array_column:
$events = [
[ 'categoryId' => 1, 'eventId' => 2, 'eventName' => 3, 'vendorName' => 4 ],
[ 'categoryId' => 5, 'eventId' => 6, 'eventName' => 7, 'vendorName' => 8 ],
[ 'categoryId' => 9, 'eventId' => 10, 'eventName' => 11, 'vendorName' => 12 ],
];
print_r(array_column($events, 'categoryId'));
See it online at 3v4l.
For versions before 5.5, you may consider using a polyfill.
There's no built in function. But one is easily made with array_map().
$array = array(
array(
"categoryID" => 1,
"CategoryName" => 2,
"EventName" => 3,
"VendorName" => 4
),
array(
"categoryID" => 5,
"CategoryName" => 6,
"EventName" => 7,
"VendorName" => 8
),
array(
"categoryID" => 9,
"CategoryName" => 10,
"EventName" => 11,
"VendorName" => 12
)
);
$newArray = array_map(function($el) {
return $el["categoryID"];
}, $array);
var_dump($newArray);
Where is lisp when you need it? Actually in php it is pretty easy to manage too. Just use the array_map function as illustrated below.
# bunch o data
$data = array();
$data[0] = array("id" => 100, "name" => 'ted');
$data[1] = array("id" => 200, "name" => 'mac');
$data[2] = array("id" => 204, "name" => 'bub');
# what you want to do to each bit of it
function pick($n) { return($n['id']); }
# what you get after you do that
$map = array_map("pick", $data);
# see for yourself
print_r($map);
You could use array_filter, and pass in a function based on the desired key.
Tadeck's answer is way better though.

Categories