I have the following multidimensional array:
array(
array(
[id] => 65,
[user_id] => 1,
[total] => 1
),
array(
[id] => 66,
[user_id] => 3,
[total] => 2
),
array(
[id] => 67,
[user_id] => 5,
[total] => 4
),
)
How do I get the array with the highest value for total and I still get the full key Value for the array like this:
array(
[id] => 67,
[user_id] => 5,
[total] => 4
)
Try this:
$total = array();
foreach ($data as $key => $row) {
$total[$key] = $row['total'];
}
// this sorts your $data array by `total` in descending order
// so the max for `total` is the first element
array_multisort($total, SORT_DESC, $data);
And now, use $data[0];
Order the arrays on id using usort():
function cmp($a, $b)
{
return strcmp($b["id"], $a["id"]);
}
usort($arr, 'cmp'); // $arr is your array
Then the first array has your desired values.
See the documentation on usort for more information: http://php.net/manual/en/function.usort.php
$array = [
['id' => 1, 'total' => 1],
['id' => 2, 'total' => 4],
['id' => 3, 'total' => 5],
];
function cmp($a, $b) {
return ($a['total'] < $b['total']) ? 1 : -1;
}
usort($array, 'cmp');
print_r($array[0]); // output ['id' => 3, 'total' => 5]
You can use array_column() with array_search():
$totals = array_column( $array, 'total' );
$result = $array[ array_search( max( $totals ), $totals ) ];
array_column() return an array with 'total' values. Use it to retrieve the key of main array with max total value.
But... what about following array?
$array = array(
array(
'id' => 65,
'user_id' => 1,
'total' => 4
),
array(
'id' => 66,
'user_id' => 3,
'total' => 2
),
array(
'id' => 67,
'user_id' => 5,
'total' => 4
),
array(
'id' => 68,
'user_id' => 6,
'total' => 2
)
);
To filter all max values, you can use this:
$max = max( array_column( $array, 'total' ) );
$result = array_filter
(
$array,
function( $row ) use ( $max )
{
return $row['total'] == $max;
}
);
print_r( $result );
Output:
Array
(
[0] => Array
(
[id] => 65
[user_id] => 1
[total] => 4
)
[2] => Array
(
[id] => 67
[user_id] => 5
[total] => 4
)
)
Put in $max variable max value of 'total' column, then use it to filter original array and retrieve all rows having it as total.
Read more about array_column()
Read more about array_filter()
You can install xdebug for var_dump
and look you array by
var_dump($array);
after you can get needing array by key
$array[3]; //example for question context
or get by foreach for analyses you array
$arrayResult = array();
foreach($array as $_arr) {
if($_arr['user_id'] == 5) {
$arrayResult = $_arr;
}
}
var_dump($arrayResult);
this is example for your question context too
Related
I have an array with the following structure:
[0] => Array
(
[venue1] => 1
[venue2] => 2
)
[1] => Array
(
[venue1] => 3
[venue2] => 4
)
[2] => Array
(
[venue1] => 2
[venue2] => 1
)
[3] => Array
(
[venue1] => 5
[venue2] => 6
)
I need to remove the duplicate "pair of values", in this case row [0] and row [2]
I tried it with that code, but it doesn't work (and of course it's not very elegant) ;-)
foreach ( $compare_arr as $v1 )
{
$key = array_search( intval($v1[venue1]), array_column( $compare_arr, 'venue2' ) );
if ( $key <> '' ) unset($compare_arr[$key]);
}
Do you have an idea how to solve this?
Thanks a lot for your help!
Oliver
Here is an approach where an intermediate array is formed of sorted values. That you can then search for to find duplicate pairs to remove.
<?php
$venues =
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
2 =>
array (
'venue1' => 2,
'venue2' => 1,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
);
$result = $pairs = $venues;
array_walk($pairs, 'sort');
var_export($pairs);
foreach($pairs as $k => $pair) {
if(count(array_keys($pairs, $pair)) > 1) {
unset($result[$k]);
}
}
var_export($result);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
),
1 =>
array (
0 => 3,
1 => 4,
),
2 =>
array (
0 => 1,
1 => 2,
),
3 =>
array (
0 => 5,
1 => 6,
),
)array (
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
If you want to remove occurring duplicates rather than pruning out duplicates altogether, you can do an array_unique on the sorted array above and then use the remaining keys to filter the original array.
$tmp = $venues;
array_walk($tmp, 'sort');
$tmp = array_unique($tmp, SORT_REGULAR);
$result = array_intersect_key($venues, $tmp);
var_export($result);
Output:
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
3 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
You might also first loop the array creating a compound key based on the ordered keys.
Then you can filter the result only keeping arrays where the count is 2 as nothing is added because there are no duplicates.
For example
$result = [];
$compare_arr = [
["venue1" => 1, "venue2" => 2],
["venue1" => 3, "venue2" => 4],
["venue1" => 2, "venue2" => 1],
["venue1" => 5, "venue2" => 6],
];
foreach ($compare_arr as $v1) {
sort($v1);
$cKey = $v1[0] .'-'. $v1[1];
if (array_key_exists($cKey, $result)) {
$result[$cKey][] = $v1;
continue;
}
$result[$cKey] = $v1;
}
$result = array_filter($result, function($item) {
return count($item) === 2;
});
print_r($result);
Output
Array
(
[3-4] => Array
(
[0] => 3
[1] => 4
)
[5-6] => Array
(
[0] => 5
[1] => 6
)
)
You can see the compound keys are the values with a - in between. If you want to have the keys numbered from 0, you can use array_values.
Php demo
Edit
If you want to keep the first matching single pair, you can check for the compound key and if it already exists continue the loop without overwriting the existing one.
$result = [];
$compare_arr = [
["venue1" => 1, "venue2" => 2],
["venue1" => 3, "venue2" => 4],
["venue1" => 2, "venue2" => 1],
["venue1" => 5, "venue2" => 6]
];
foreach ($compare_arr as $v1) {
sort($v1);
$cKey = $v1[0] .'-'. $v1[1];
if (array_key_exists($cKey, $result)) {
continue;
}
$result[$cKey] = $v1;
}
print_r($result);
Output
Array
(
[1-2] => Array
(
[0] => 1
[1] => 2
)
[3-4] => Array
(
[0] => 3
[1] => 4
)
[5-6] => Array
(
[0] => 5
[1] => 6
)
)
Php demo
Whether you use a classic foreach() loop or functional iteration, there is no reason to iterate the input array more than once.
This snippet will appear nearly identical to TheFourthBird's answer, but I don't like the unnecessary use of continue. This snippet will ensure no that rows in the result array have 100% shared venue values (in any order). The subarray keys will also not suffer reordering; in other words the first element key will be venue1 then the second element will be venue2. Using implode() offers additional flexibility because the code won't need to be altered if the number of elements in each row changes.
$result = [];
foreach ($data as $index => $row) {
sort($row);
$key = implode('-', $row);
if (!isset($result[$key])) {
$result[$key] = $data[$index];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'venue1' => 1,
'venue2' => 2,
),
1 =>
array (
'venue1' => 3,
'venue2' => 4,
),
2 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
To completely remove all rows where venue values are shared, maintain a "found" array as well as a "result" array.
Code: (Demo)
$result = [];
foreach ($data as $index => $row) {
sort($row);
$key = implode('-', $row);
if (!isset($found[$key])) {
$found[$key] = true;
$result[$key] = $data[$index];
} else {
unset($result[$key]);
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'venue1' => 3,
'venue2' => 4,
),
1 =>
array (
'venue1' => 5,
'venue2' => 6,
),
)
I'm having this array:
array(
[1] => Array
(
[1111] => Array
(
[one] => 70
[two] => 7.0
)
)
[2] => Array
(
[1111] => Array
(
[one] => 10
[two] => 2.0
)
)
)
And i want it to sum [one] and [two] of the two different arrays and output an array like this:
Array
(
[1111] => Array (
[one] => 80
[two] => 9.0
)
)
Based on the key "1111"
I have tried with the following
$array = array_values( $array );
$sum_array = [];
foreach ( $array as $k => $sub_array ) {
foreach ( $sub_array as $id => $value ) {
$sum_array[$id] += array_key_exists( $id, $sum_array ) ? $sum_array[$id] += $value : $sum_array[$id] = $value;
$sum_array[$id]['two'] += array_sum( $sum_array[$id]['two'] ) / ( 100 * count( $sum_array[$id]['count'] ) );
}
}
But that gives me only the first value like this:
Array
(
[1111] => Array (
[one] => 70
[two] => 7.0
)
)
Can someone please tell me what i'm doing wrong?
You could use the array_reduce to reduce your array to only one element.
The array_reduce function takes two ( sometime three ) parameters. The first one is the array, the second one is a callback with two parameters , the collector, and the current item. If there is a third parameters to the array_reduce function, it will be used to initialize the collector ( like in our case ).
The function will parse each item and do something with it, the collector will be available at each iteration, that's why we use it to keep information of the data, in our case, it's the sum of the values. Once all the item has been iterated over, the function returns the collector.
<?php
$array = [
[
'1111' => [
'one' => 70,
'two' => 7.0
]
],
[
'1111' => [
'one' => 10,
'two' => 2.0
]
]
];
$output = array_reduce($array, function($collector, $item) {
$collector['1111']['one'] += $item['1111']['one'];
$collector['1111']['two'] += $item['1111']['two'];
return $collector;
}, ['1111' => ['one' => 0, 'two' => 0]]);
var_dump($output);
Here is an approach that uses a recursive merge. In the following when merged John's hours become an array. We then need to sum. As James only has one entry his hours do not become an array so we leave them be.
Hopefully the transitional merge output, will give you an idea of the process:
<?php
$items =
[
[
'john' => [
'hours' => 2,
'tips' => 7
]
],
[
'john' => [
'hours' => 3,
'tips' => 10
]
],
[
'james' => [
'hours' => 8,
'tips' => 0
]
]
];
$output = array_merge_recursive(...$items);
var_export($output);
array_walk($output, function(&$v) {
array_walk($v, function(&$v) {
if(is_array($v))
$v = array_sum($v);
});
});
echo "\nResult: \n";
var_export($output);
Output:
array (
'john' =>
array (
'hours' =>
array (
0 => 2,
1 => 3,
),
'tips' =>
array (
0 => 7,
1 => 10,
),
),
'james' =>
array (
'hours' => 8,
'tips' => 0,
),
)
Result:
array (
'john' =>
array (
'hours' => 5,
'tips' => 17,
),
'james' =>
array (
'hours' => 8,
'tips' => 0,
),
)
$total=0;
array_map(function ($item) use ($total)
{
$total += $item['one'] + $item['two'];
}, $array);
echo $total;
Or difference key
$one = array_sum(array_column($array, 'one'));
$two = array_sum(array_column($array, 'two'));
$resultArray = compact('one','two');
```
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
I have the following array:
$array = [
['id' => 1, 'code' => '342'],
['id' => 2, 'code' => '765'],
['id' => 3, 'code' => '134'],
['id' => 1, 'code' => '999'],
];
Here Array[0]['id'] and Array[3]['id'] are duplicates with respect to ['id']. I want to remove one of them (doesn't matter which).
Actually I thought I found a solution with this code:
//data
$row = $stmt->fetchALL(PDO::FETCH_ASSOC);
$ids = array();
for($i = 0;$i < count($row); $i++ )
{
if (in_array($row[$i]['id'], $ids))
{
unset($row[$i]);
continue;
}
$ids[] = $row[$i]['id'];
}
print_r($row);
For some reason it works well with small arrays, but if I have a lot of values in it with multiple duplicates, it fails. Any suggestion to what I am missing?
Simple solution using regular foreach loop:
$rows = [
['id' => 1, 'code' => '342'],
['id' => 2, 'code' => '765'],
['id' => 3, 'code' => '134'],
['id' => 1, 'code' => '342']
];
$ids = [];
foreach ($rows as $k => $item) {
if (in_array($item['id'], $ids)){
unset($rows[$k]);
} else {
$ids[] = $item['id'];
}
}
print_r($rows);
I think this can be done without looping. Let me show an example:
$rows = array (
array ('id' => 1, 'code' => '342'),
array ('id' => 2, 'code' => '765'),
array ('id' => 3, 'code' => '134'),
array ('id' => 1, 'code' => '342')
);
$input = array_map("unserialize", array_unique(array_map("serialize", $rows)));
echo '<pre>';
print_r($input);
Output:
Array
(
[0] => Array
(
[id] => 1
[code] => 342
)
[1] => Array
(
[id] => 2
[code] => 765
)
[2] => Array
(
[id] => 3
[code] => 134
)
)
I want to remove one of them (doesn't matter which).
Okay, use array_column() to assign temporary first level keys using the id values. Because PHP will not allow two elements in the same level to share the same key, each subsequent duplicate will overwrite the earlier encountered row. Wrap that call in array_values() to remove the temporary keys (re-index the array).
Code: (Demo)
var_export(
array_values(array_column($rows, null, 'id'))
);
Output:
array (
0 =>
array (
'id' => 1,
'code' => '999',
),
1 =>
array (
'id' => 2,
'code' => '765',
),
2 =>
array (
'id' => 3,
'code' => '134',
),
)
I have sorted a multidimensional array on value distance from low to high. This is an example of the output: (the actual output has around 20 or 30 arrays).
Array
(
[0] => Array
(
[id] => 1
[distance] => 5
[sponsor] => 0
)
[1] => Array
(
[id] => 20
[distance] => 8
[sponsor] => 1
)
[2] => Array
(
[id] => 25
[distance] => 10
[sponsor] => 0
)
[3] => Array
(
[id] => 78
[distance] => 25
[sponsor] => 1
)
)
After sorting on distance from low to high, I want to give priority if sponsor = 1. This is the preferred output:
Array
(
[0] => Array
(
[id] => 20
[distance] => 8
[sponsor] => 1
)
[1] => Array
(
[id] => 78
[distance] => 25
[sponsor] => 1
)
[2] => Array
(
[id] => 1
[distance] => 5
[sponsor] => 0
)
[3] => Array
(
[id] => 25
[distance] => 10
[sponsor] => 0
)
)
Sponsor is either 0 or 1. How can I tackle this problem? I was thinking of, before sorting on distance, I should split up the array in 2 arrays based on sponsor (0,1), sort both arrays on distance, and then merge them with sponsor=1 at the top of the new multidimensional array. Is this the way to do it?
Thank you for your input.
This is slightly modified version of what usort suggests. Assuming source array is called $arr:
usort(
$arr, function($a, $b) {
if ( $a['sponsor'] == $b['sponsor'] ) {
if ( $a['distance'] == $b['distance'] ) {
return 0;
}
return $a['distance'] < $b['distance'] ? -1 : 1;
}
return $a['sponsor'] > $b['sponsor'] ? -1 : 1;
}
);
You have to split original array to 2 arrays: 1st has sponsor=1, 2nd has sponsor=0. Then sort them separately and merge.
Somehow like this:
$array1 = array_filter($array, function ($v) { return $v['sponsor'] == 1; });
$array2 = array_filter($array, function ($v) { return $v['sponsor'] == 0; });
function cmp($a, $b) {
return $a['distance'] < $b['distance']? -1 : 1;
}
usort($array1, cmp);
usort($array2, cmp);
$result = array_merge($array1, $array2);
You can use array_multisort() function to achieve what you want:
<?php
$arr = [
['id' => 1, 'distance' => 5, 'sponsor' => 0],
['id' => 20, 'distance' => 8, 'sponsor' => 1],
['id' => 25, 'distance' => 10, 'sponsor' => 0],
['id' => 78, 'distance' => 25, 'sponsor' => 1],
];
$sponsor = array_column($arr, 'sponsor');
$distance = array_column($arr, 'distance');
// rearrange $arr by sponsor DESC and then by distance ASC
array_multisort($sponsor, SORT_DESC, $distance, SORT_ASC, $arr);
print_r($arr);
That would work but if you use a stable sorting algorithm then you can sort first by distance, then by sponsor descending and you will get your desired results.
Try This :
<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
or
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
I have an array of subarrays in the following format:
[
'a' => ['id' => 20, 'name' => 'chimpanzee'],
'b' => ['id' => 40, 'name' => 'meeting'],
'c' => ['id' => 20, 'name' => 'dynasty'],
'd' => ['id' => 50, 'name' => 'chocolate'],
'e' => ['id' => 10, 'name' => 'bananas'],
'f' => ['id' => 50, 'name' => 'fantasy'],
'g' => ['id' => 50, 'name' => 'football']
]
And I would like to group it into a new array based on the id field in each subarray.
array
(
10 => array
(
e => array ( id = 10, name = bananas )
)
20 => array
(
a => array ( id = 20, name = chimpanzee )
c => array ( id = 20, name = dynasty )
)
40 => array
(
b => array ( id = 40, name = meeting )
)
50 => array
(
d => array ( id = 50, name = chocolate )
f => array ( id = 50, name = fantasy )
g => array ( id = 50, name = football )
)
)
$arr = array();
foreach ($old_arr as $key => $item) {
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
foreach($array as $key => $value){
$newarray[$value['id']][$key] = $value;
}
var_dump($newarray);
piece of cake ;)
The following code adapts #Tim Cooper’s code to mitigate Undefined index: id errors in the event that one of the inner arrays doesn’t contain an id:
$arr = array();
foreach($old_arr as $key => $item)
{
if(array_key_exists('id', $item))
$arr[$item['id']][$key] = $item;
}
ksort($arr, SORT_NUMERIC);
However, it will drop inner arrays without an id.
E.g.
$old_arr = array(
'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' ),
'h' => array ( 'name' => 'bob' )
);
will drop the 'h' array completely.
You can also use Arrays::groupBy() from ouzo-goodies:
$groupBy = Arrays::groupBy($array, Functions::extract()->id);
print_r($groupBy);
And result:
Array
(
[20] => Array
(
[0] => Array
(
[id] => 20
[name] => chimpanzee
)
[1] => Array
(
[id] => 20
[name] => dynasty
)
)
[40] => Array
(
[0] => Array
(
[id] => 40
[name] => meeting
)
)
[50] => Array
(
[0] => Array
(
[id] => 50
[name] => chocolate
)
[1] => Array
(
[id] => 50
[name] => fantasy
)
[2] => Array
(
[id] => 50
[name] => football
)
)
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => bananas
)
)
)
And here are the docs for Arrays and Functions.
Here is a function that will take an array as the first argument and a criteria (a string or callback function) as the second argument. The function returns a new array that groups the array as asked for.
/**
* Group items from an array together by some criteria or value.
*
* #param $arr array The array to group items from
* #param $criteria string|callable The key to group by or a function the returns a key to group by.
* #return array
*
*/
function groupBy($arr, $criteria): array
{
return array_reduce($arr, function($accumulator, $item) use ($criteria) {
$key = (is_callable($criteria)) ? $criteria($item) : $item[$criteria];
if (!array_key_exists($key, $accumulator)) {
$accumulator[$key] = [];
}
array_push($accumulator[$key], $item);
return $accumulator;
}, []);
}
Here is the given array:
$arr = array(
'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
'b' => array ( 'id' => 40, 'name' => 'meeting' ),
'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
'e' => array ( 'id' => 10, 'name' => 'bananas' ),
'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
'g' => array ( 'id' => 50, 'name' => 'football' )
);
And examples using the function with a string and a callback function:
$q = groupBy($arr, 'id');
print_r($q);
$r = groupBy($arr, function($item) {
return $item['id'];
});
print_r($r);
The results are the same in both examples:
Array
(
[20] => Array
(
[0] => Array
(
[id] => 20
[name] => chimpanzee
)
[1] => Array
(
[id] => 20
[name] => dynasty
)
)
[40] => Array
(
[0] => Array
(
[id] => 40
[name] => meeting
)
)
[50] => Array
(
[0] => Array
(
[id] => 50
[name] => chocolate
)
[1] => Array
(
[id] => 50
[name] => fantasy
)
[2] => Array
(
[id] => 50
[name] => football
)
)
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => bananas
)
)
)
Passing the callback is overkill in the example above, but using the callback finds its use when you pass in an array of objects, a multidimensional array, or have some arbitrary thing you want to group by.
Maybe it's worth to mention that you can also use php array_reduce function
$items = [
['id' => 20, 'name' => 'chimpanzee'],
['id' => 40, 'name' => 'meeting'],
['id' => 20, 'name' => 'dynasty'],
['id' => 50, 'name' => 'chocolate'],
['id' => 10, 'name' => 'bananas'],
['id' => 50, 'name' => 'fantasy'],
['id' => 50, 'name' => 'football'],
];
// Grouping
$groupedItems = array_reduce($items, function ($carry, $item) {
$carry[$item['id']][] = $item;
return $carry;
}, []);
// Sorting
ksort($groupedItems, SORT_NUMERIC);
print_r($groupedItems);
https://www.php.net/manual/en/function.array-reduce.php
Because of how PHP's sorting algorithm treats multidimensional arrays -- it sorts by size, then compares elements one at a time, you can actually use a key-preserving sort on the input BEFORE restructuring. In functional style programming, this means that you don't need to declare the result array as a variable.
Code: (Demo)
asort($array);
var_export(
array_reduce(
array_keys($array),
function($result, $k) use ($array) {
$result[$array[$k]['id']][$k] = $array[$k];
return $result;
}
)
);
I must say that functional programming is not very attractive for this task because the first level keys must be preserved.
Although array_walk() is more succinct, it still requires the result array to be passed into the closure as a reference variable. (Demo)
asort($array);
$result = [];
array_walk(
$array,
function($row, $k) use (&$result) {
$result[$row['id']][$k] = $row;
}
);
var_export($result);
I'd probably recommend a classic loop for this task. The only thing the loop needs to do is rearrange the first and second level keys. (Demo)
asort($array);
$result = [];
foreach ($array as $k => $row) {
$result[$row['id']][$k] = $row;
}
var_export($result);
To be completely honest, I expect that ksort() will be more efficient than pre-loop sorting, but I wanted to a viable alternative.