Get lowest key - array value in multidimensional array PHP - php

So, I got array that looks something like this:
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
float(234)
["sp"]=>
float(234)
}
[1]=>
array(2) {
["p"]=>
float(53)
["sp"]=>
float(5)
}
[2]...
[3]...
}
The idea is to go through each of 0 - N values of key 65 array, and only keep one with smallest "p", others should be removed / filtered out.
This should be done in PHP.
Anyone has any idea?
I tried something like this:
$array = array_filter($array, function ($value, $key) use ($a) {
return $a['p'] <= $value['p'];
}, ARRAY_FILTER_USE_BOTH);
where $value is 1 of elements inside 65 keyed-array and $a is current pair that is being added dynamically. So when ever its added, I go through existing elements and if its lowest, it should stay, and others get instant filtered out, but if its higher, it should automatically be filtered out.
Thank you!

You can use array_reduce() to get the lowest "p"-value:
$arr = [
65 => [
["p" => 234, "sp" => 234],
["p" => 53, "sp" => 5],
["p" => 530, "sp" => 5],
]
];
function getLowestKey($carry, $item) {
if ($item['p'] < $carry || !$carry) {
$carry = $item['p'];
}
return $carry;
}
$lowestKey = array_reduce($arr[65], 'getLowestKey');
var_dump($lowestKey); // int(53)
Edit:
I just noticed there is a second part to your question, sorry about that. Once you found out the "lowest p" you can then just filter the array with that knowledge:
$lowestPs = array_filter($arr[65], function($item) use ($lowestKey) {
return $item['p'] == $lowestKey;
});
var_dump($lowestPs);
/*
array(2) {
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[2]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
}
*/
This solution works even if multiple entries have the same lowest "p" value (like 53 in the above example), all of those will stay.

Use array_column() to do an array_multisort() on the 'p' value for the records inside key 65.
<?php
$col = 'p'; // set the column you want to order on
$column = array_column($arr[65], $col);
array_multisort($column, SORT_ASC, $arr[65]);
$arr[65] = $arr[65][0]; // only keep the record with lowest 'p' value
demo

If have more than 1 nested levels, you might also use a recursive approach checking the value of p, keeping the array with the lowest value.
$arrays = [
65 => [
["p" => 234, "sp" => 234],
[
["p" => 53,"sp" => 5],
[
["p" => 54,"sp" => 1],
["p" => 53,"sp" => 7],
]
], [
"p" => 255,
"sp" => 235
],
]
];
function loop($array, &$coll = [], &$min = null)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
loop($value, $coll, $min);
} elseif ($key === "p") {
if ($min === null) $min = $value;
if ($min > $value) {
$coll = [$array];
$min = $value;
continue;
}
if($value === $min) $coll[] = $array;
}
}
return $coll;
}
$arrays[65] = loop($arrays[65]);
var_dump($arrays);
Output
array(1) {
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(7)
}
}
}
See another php demo.

Related

search or filter php array for a particular value and return outer index

I have defined a multi-dimensional array in php and I want to get the index of the 1st level, where a specific inner key has a certain value:
$arr = [
[ [ "a" => "one", "b" => "two", ],
[ "a" => "three", "b" => "four", ],
],
[ [ "a" => "five", "b" => "six", ],
[ "a" => "seven", "b" => "eight", ],
],
];
I want to get the index $i of the 1st level, where $arr[$i][$j]["b"] == "six".
Thus $arr[$i] should resolve to:
array(2) {
[0]=>
array(2) {
["a"]=>
string(4) "five"
["b"]=>
string(3) "six"
}
[1]=>
array(2) {
["a"]=>
string(5) "seven"
["b"]=>
string(5) "eight"
}
}
I could do this with a loop:
function getIndex($needle, $haystack) {
for ($i = 0; $i < count($haystack); $i++) {
for ($j = 0; $j < count($haystack[$i]); $j++) {
if ( $haystack[$i][$j]['b'] == $needle ) {
return $i;
}
}
}
}
$x = $arr[ getIndex('six', $arr) ];
var_dump($x);
Can I achieve this without interation loops? With an expression to filter or search the array, like
$x = $arr[ <expression> ];
A flexible way of trying to achieve this would be to pass a callback to the search function. This callback would be used to filter the data, only if there is a match will there be some data and this can be used to return the index where it matches.
So first a callback which would check the element of the array...
function check($haystack)
{
return $haystack['b'] == 'six';
}
Then the function itself...
function getIndex(callable $filter, array $haystack)
{
// Loop the base level of the array
foreach ($haystack as $index => $subArray) {
// If applying the filter method leaves some data, then it's found
if (array_filter($subArray, $filter)) {
return $index;
}
}
// Return false for not found
return false;
}
Then the actual usage
var_dump(getIndex('check', $arr));

How to merge subarrays using keys and sum the values?

I'm fairly new to PHP and I'm having some trouble with arrays and combining data. I have the following array which has been created from a foreach loop:
array(1) {
[36868]=> int(3)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[36868]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The key in each array is a user ID, so I need to preserve this, but I only want one instance of each key and where there are duplicate keys, sum the values. Like so:
array(1) {
[36868]=> int(6)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The I've tried looping through the array:
foreach ($array as $key => &$value) {
if ($value[0] == $value[1]) {
$value[1] += $value[1];
}
}
But with no luck. I've also tried rendering the arrays differently i.e. [userid]=>1,[score]=>3 and I feel like I'm going round in circles a bit, so any help would be hugely appreciated.
$data <-- this is your original array
$result = array_reduce(
$data,
function($carry, $item) {
foreach ($item as $id => $score) {
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
}
return $carry;
},
[]
);
If you are sure that each item only contains 1 entry you could also simplify the callback to not use foreach:
$result = array_reduce(
$data,
function ($carry, $item) {
$score = reset($item);
$id = key($item);
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
return $carry;
},
[]
);
You could also keep using foreach instead:
/** foreach to create a $data array like described below and afterwards do this: **/
$result = [];
foreach($data as $row) {
$score = reset($row);
$id = key($row);
if (array_key_exists($id, $result)) {
$result[$id] += $score;
} else {
$result[$id] = $score;
}
}
This will take an array $data like this:
array(
array('1' => 3),
array('1' => 3),
array('2' => 3),
);
and creates the variable $result like this:
array(
'1' => 6,
'2' => 3,
);
Here is a clean method that will not produce Notices. When merge-summing array data the efficient method is to generate temporary keys and use the very fast isset() function. I could have used current() and key() to access the lone subarray element, but the second foreach control structure is actually faster and more compact. (Ref:
https://stackoverflow.com/a/21219594/2943403 )
Code: (Demo)
$array = [
[36868 => 3],
[2112 => 3],
[35901 => 3],
[6496 => 3],
[87 => 3],
[36868 => 3],
[68 => 3],
[9068 => 3],
[47 => 3]
];
$result = [];
foreach ($array as $subarray) {
foreach ($subarray as $k => $v) {
if (!isset($result[$k])) {
$result[$k] = $subarray;
} else {
$result[$k][$k] += $v;
}
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
36868 => 6,
),
1 =>
array (
2112 => 3,
),
2 =>
array (
35901 => 3,
),
3 =>
array (
6496 => 3,
),
4 =>
array (
87 => 3,
),
5 =>
array (
68 => 3,
),
6 =>
array (
9068 => 3,
),
7 =>
array (
47 => 3,
),
)

Sum values of array by key in PHP

I need to find array values by key and sum this values if key exists in other array. I tried to different combination but I havent a good idea.
array(2) {
["www.test.pl"]=>
array(3) {
["category"]=>
array(3) {
}
["category2"]=>
array(3) {
}
}
["www.test2.pl"]=>
array(3) {
["category"]=>
array(3) {
}
["category2"]=>
array(3) {
}
["category3"]=>
array(3) {
}
}
}
I need to compare keys -"category", "category2" Of every URL ... and sum values if I have keys in both array of URLs.
I tries to do this in this example
link to compare array code
You can summ values in ne array:
// $arr1 - starting array
$arr2 = [];
foreach ($arr1 as $arr){
foreach ($arr as $arKey => $arVal) {
if (isset($arr2[$arKey])) {
$arr2[$arKey]['clicks'] += $arVal['clicks'];
$arr2[$arKey]['impressions'] += $arVal['impressions'];
$arr2[$arKey]['ctr'] += $arVal['ctr'];
} else {
$arr2[] = [
'clicks' => $arVal['clicks'],
'impressions' => $arVal['impressions'],
'ctr' => $arVal['ctr'],
];
}
}
}
I hope it is what you want to do
Like this:
foreach($array as $key => $value){
if(isset($array2[$key])){
$sum = $array2[$key]+$value;
}
}

Referenced array iteration in php results in strange result

I have seen strange behavior that I don't quite get. I do the following:
$array = [
'a' => [
'a1' => [
'a11' => 1,
'a12' => 2
],
'a2' => [
'a21' => 3,
'a22' => 4
],
],
'b' => [
'b1' => [
'b11' => 1,
'b12' => 2
],
'b2' => [
'b21' => 3,
'b22' => 4
],
],
];
foreach ($array as $strLevel1 => &$arrLevel1)
{
foreach ($arrLevel1 as $strLevel2 => &$arrLevel2)
{
foreach ($arrLevel2 as $strLevel3 => &$varLevel3)
{
$varLevel3 = 0;
}
}
}
echo '<pre>';
var_dump($array);
echo '</pre>';
foreach ($array as $strLevel1 => $arrLevel1)
{
}
echo '<pre>';
var_dump($array);
echo '</pre>';
The result is as follows:
array(2) {
["a"]=>
array(2) {
["a1"]=>
array(2) {
["a11"]=>
int(0)
["a12"]=>
int(0)
}
["a2"]=>
array(2) {
["a21"]=>
int(0)
["a22"]=>
int(0)
}
}
["b"]=>
&array(2) {
["b1"]=>
array(2) {
["b11"]=>
int(0)
["b12"]=>
int(0)
}
["b2"]=>
&array(2) {
["b21"]=>
int(0)
["b22"]=>
&int(0)
}
}
}
array(2) {
["a"]=>
array(2) {
["a1"]=>
array(2) {
["a11"]=>
int(0)
["a12"]=>
int(0)
}
["a2"]=>
array(2) {
["a21"]=>
int(0)
["a22"]=>
int(0)
}
}
["b"]=>
&array(2) {
["a1"]=>
array(2) {
["a11"]=>
int(0)
["a12"]=>
int(0)
}
["a2"]=>
array(2) {
["a21"]=>
int(0)
["a22"]=>
int(0)
}
}
}
As you can see, in the first output everything is ok. But in the second one, the b-named branch of the array is replaced by the a-named branch. This is because of the referencing I did. If I put a "&" before $arrLevel1 in the last loop, it works again.
Why is that? Am I doing something wrong with the references? Or should I don't use them at all and do array manipulation only fully qualified without any reference?
Thanks in advance.
Maybe you should unset the reference to $arrLevel1 after your loop, till you reused the var for the second loop.
foreach ($array as $strLevel1 => &$arrLevel1)
{
foreach ($arrLevel1 as $strLevel2 => &$arrLevel2)
{
foreach ($arrLevel2 as $strLevel3 => &$varLevel3)
{
$varLevel3 = 0;
}
}
}
// remove reference
unset($arrLevel1);
echo '<pre>';
var_dump($array);
echo '</pre>';
foreach ($array as $strLevel1 => $arrLevel1)
{
}
echo '<pre>';
var_dump($array);
echo '</pre>';
A way to come around unset is to use unique names for your loop variables or - if you want to loop over the same array again - just also use an reference loop var.
foreach ($array as $strLevel1 => &$arrLevel1)
{
foreach ($arrLevel1 as $strLevel2 => &$arrLevel2)
{
foreach ($arrLevel2 as $strLevel3 => &$varLevel3)
{
$varLevel3 = 0;
}
}
}
// also use a reference
foreach ($array as $strLevel1 => &$arrLevel1)
{
}
Most of the time, it might be simplier to just don't use references or move the loop in an "atomic" function which just does the manipulation and returns the result.

uasort is removing keys/arrays

I'm trying to sort a multidimensional array (in fact that it is multidimensional doesn't matter since I'm always only sorting one dimension).
function orderSort($a, $b) {
if ($a['order'] == $b['order']) return 0;
return($a['order'] < $b['order']) ? -1 : 1;
}
$nav = array(
"section" => array(
"header" => array(),
"main" => array()
),
"link" => array(
"header" => array(),
"main" => array()
)
);
$DATA = array(
array(
"type" => "section",
"subtype" => "main",
"data" => array("name" => "/Basic", "order" => 1, "parent" => "bbb")
),
array(
"type" => "link",
"subtype" => "main",
"data" => array("name" => "Home", "link" => array("/"), "order" => 1, "parent" => "/Basic")
)
);
foreach($DATA as $ele) {
if(!array_key_exists($ele['data']['parent'], $nav[$ele['type']][$ele['subtype']]))
$nav[$ele['type']][$ele['subtype']][$ele['data']['parent']] = array($ele['data']);
else
array_push($nav[$ele['type']][$ele['subtype']][$ele['data']['parent']], $ele['data']);
}
var_dump($nav['section']['main']);
echo '<br><br>';
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
var_dump($nav['section']['main']);
The first var_dump returns:
array(1) {
["bbb"]=> array(1) {
[0]=> array(3) {
["name"]=> string(6) "/Basic"
["order"]=> int(1) ["parent"]=> string(3) "bbb"
}
}
}
As you can see there is a key named bbb (Sorry for that... highly conceptional). Now the second output:
array(1) {
[0]=> array(3) {
["name"]=> string(6) "/Basic"
["order"]=> int(1)
["parent"]=> string(3) "bbb"
}
}
Suddenly bbb is gone. In fact the whole array (with only a single element) has disappeared.
My question is, can I somehow prevent this from happening? I never asked for this kind of of optimization. Of course sorting this with only one element doesn't make any sense but still.
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
Okay. And which is the key? you just forgot the keys (you are overwriting the parent of the original array with a new array instead of the original array itself):
foreach($nav['section']['main'] as $key => $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'][$key] = $ele;
}
would be correct, but by reference is even better:
foreach($nav['section']['main'] as &$ele) {
uasort($ele, 'orderSort');
}
Your problem is here:
foreach($nav['section']['main'] as $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'] = $ele;
}
This needs to be
foreach($nav['section']['main'] as $index => $ele) {
uasort($ele, 'orderSort');
$nav['section']['main'][$index] = $ele;
}
You were replacing the whole $nav['section']['main'] with the first element.

Categories