I am currently trying to sort a multidimensional array by its totalPoints. Each array lineupSet has an n amount of items. I am trying to get the lineupSet with the the highest total totalPoints. How could I achieve most efficiently? the below is sudocode and therefore not working. Unsure how to approach this.
Code
public function getHighestTotalPoints($testArray)
{
if (isset($testArray) && !empty($testArray)) {
uasort($testArray, function ($a, $b) {
return $a['lineupSet']['formula']['totalPoints'] <=> $b['lineupSet']['formula']['totalPoints'] ;
});
return array_reverse($testArray);
}
return null;
}
$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "test2",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "test3",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 220.66,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 214.76,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "test2",
]],
],
]];
Desired result:
[0] => Array
(
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)
[name] => test1
)
[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)
[name] => test2
)
)
)
)
You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:
function sum_points($v) {
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset) {
if (isset($lset['formula'])) {
$totalPoints += $lset['formula']['totalPoints'];
}
else {
foreach ($lset as $l) {
$totalPoints += $l['formula']['totalPoints'];
}
}
}
return $totalPoints;
}
To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:
function sort_points($a, $b) {
return sum_points($b) - sum_points($a);
}
Finally we call usort with this function and output the first element of the array:
usort($testArray, 'sort_points');
print_r($testArray[0]);
Output:
Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)
[name] => test1
)
[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)
[name] => test2
)
)
)
)
Demo on 3v4l.org
Related
I need to modify an array with subarrays and keep only the top two arrays (array -> results - x) and remove any subarray below. For example array "location" & "syncState" should be removed.
Original array:
$device_array = [
"totalCount" => "3",
"results" => [
[
"id" => "2",
"serialNumber" => "DX",
"location" => ["id" => "5", "locationName" => "US"]
],
[
"id" => "4",
"serialNumber" => "DM",
"syncState" => ["id" => "7", "locationName" => "DE"]
],
[
"id" => "5",
"serialNumber" => "C0"
]
]
];
The array should look like this:
Array
(
[totalCount] => 3
[results] => Array
(
[0] => Array
(
[id] => 2
[serialNumber] => DX
)
[1] => Array
(
[id] => 4
[serialNumber] => DM
)
[2] => Array
(
[id] => 5
[serialNumber] => C0
)
)
)
I'm trying to loop through the arrays (sub arrays included) but I can't remove all of the subarrays that sit under $device_array['results'][x].
foreach ($device_array as $key => $value) {
if(is_array($value)) {
unset($device_array['results'][0]['location']);
}
}
You can just loop the results subarray directly and write a custom filter which will modify each entry by reference. Any of the associative elements that hold array type data will be filtered out.
Code: (Demo)
$array = [
"totalCount" => "3",
"results" => [
[
"id" => "2",
"serialNumber" => "DX",
"location" => ["id" => "5", "locationName" => "US"]
],
[
"id" => "4",
"serialNumber" => "DM",
"syncState" => ["id" => "7", "locationName" => "DE"]
],
[
"id" => "5",
"serialNumber" => "C0"
]
]
];
foreach ($array['results'] as &$entry) {
$entry = array_filter($entry, 'is_scalar');
}
var_export($array);
Output:
array (
'totalCount' => '3',
'results' =>
array (
0 =>
array (
'id' => '2',
'serialNumber' => 'DX',
),
1 =>
array (
'id' => '4',
'serialNumber' => 'DM',
),
2 =>
array (
'id' => '5',
'serialNumber' => 'C0',
),
),
)
Or completely functional style: (Demo)
$array['results'] = array_map(
function($entry) {
return array_filter($entry, 'is_scalar');
},
$array['results']
);
var_export($array);
This is how you obtain the output, but I am not so sure if this is what you need in your case
<?php
$array = [
'total' => 2,
'result' => [
[
'id' => 1,
'serialNumber' => 'DX',
'location' => ['id'=>1, 'locationName'=>'US']
],
[
'id' => 2 ,
'serialNumber' => 'DO',
'syncState' => ['id'=>7, 'locationName'=>'DE']
]
]
];
foreach( $array['result'] as $key => $value ){
foreach($value as $key2=>$subarray){
if(is_array($subarray)){
unset($value[$key2]);
}
}
$array['result'][$key] = $value;
}
print_r($array);
I have an array like the one below
array:4 [▼
0 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "Keyword B3"
]
1 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "[IMAGES 1]"
]
]
1 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "D1"
]
1 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "E3"
]
]
]
I want to combine the above 2 arrays into 1 array as follows (number of items in 2 arrays is always equal):
$result = [
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
]
"value" => [
"Keyword B3",
"D1"
]
],
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
],
"value" => [
"[IMAGES 1]",
"E3"
]
]
]
I have searched a lot but have not found a solution yet. Thanks for any help.
Because number of items in 2 sub-arrays is always equal, you can use a for loop like this:
$array = [
0 => [
0 => [
'url' => 'https://domain.com.vn',
'value' => 'Keyword B3'
],
1 => [
'url' => 'https://domain.com.vn',
'value' => '[IMAGES 1]'
]
],
1 => [
0 => [
'url' => 'https://domain-4.com.vn',
'value' => 'D1'
],
1 => [
'url' => 'https://domain-4.com.vn',
'value' => 'E3'
]
]
];
$result = [];
for ($i = 0 ; $i < count($array[0]); $i++) {
$result[] = [
'url' => [$array[0][$i]['url'], $array[1][$i]['url']],
'value' => [$array[0][$i]['value'], $array[1][$i]['value']],
];
}
print_r($result);
Result:
Array
(
[0] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => Keyword B3
[1] => D1
)
)
[1] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => [IMAGES 1]
[1] => E3
)
)
)
For a dynamic solution, you will need to iterate the first level of the array, then combine the first deep subarray's keys with the return value of array_map()'s special transposing behavior with null and the spread operator.
It sure is a bunch of voodoo, but it is dynamic.
Code: (Demo) (Demo showing different length first level data sets)
var_export(
array_map(
function ($subarray) {
return array_combine(
array_keys($subarray[0]),
array_map(null, ...array_values($subarray))
);
},
$array
)
);
See other other phptranspose tagged Stack Overflow pages to see other usages of the transposing technique.
I have 2 arrays in php which look like following:
array1 = [product_id = [data], product_id = [data], ..]
[
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
],
...
]
array2 = attribute of product with product_id
[
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
],
...
]
What I want is according to product_id in array2 data is pushed in array1 as child array like this
[
101 => [
"sku" => "AB01",
"attributes" => [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
]
]
],
201 => [
"sky" => "AB02",
"attributes" => [
0 => [
"product_id" => 201,
"name" => "pro 2"
],
1 => [
"product_id" => 201,
"size" => "S"
]
]
],
...
]
Array length is around 1000 for array1 and >5000 for array2. foreach loop taking too much time. Is there any fast way to achieve it?
About the fastest way I can think of achieving this is using a foreach loop over the second array, as your first array is indexed by the product_id, it is easy to directly insert the new data on each loop...
foreach ( $array2 as $item ) {
$array1[$item['product_id']]['attributes'][] = $item;
}
Maybe it helps you
$array1 = [
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
]
];
$array2 = [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
]
] ;
foreach ($array2 as $result){
if(array_key_exists($result['product_id'], $array1)) {
$array1[$result['product_id']]['attributes'][] = $result;
}
}
print_r($array1);
your result look like this
Array
(
[101] => Array
(
[sku] => AB01
[attributes] => Array
(
[0] => Array
(
[product_id] => 101
[name] => pro 1
)
[1] => Array
(
[product_id] => 101
[size] => S
)
)
)
[201] => Array
(
[sky] => AB02
[attributes] => Array
(
[0] => Array
(
[product_id] => 201
[name] => pro 2
)
[1] => Array
(
[product_id] => 201
[size] => S
)
)
)
)
$arr1=[
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
]
];
$arr2=[
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
]
];
foreach ($arr2 as $arr_val ) {
$arr1[$arr_val['product_id']]['attributes'][] = $arr_val;
}
echo "<pre>"; print_r($arr1);
I hope this answer work for you
first array
$arr = [
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
],
];
second array
$arr2 = [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
],
];
finally this what i try to do
$newArr = [];
forEach($arr as $product_id => $product_value){
$attrArr = [];
forEach($arr2 as $key => $value){
if($product_id == $value['product_id']){
$attrArr[] = $value;
$newArr[$product_id] = [
array_keys($product_value)[0] => array_values($product_value)[0],
];
}
}
$newArr[$product_id]['attributes'] = $attrArr;
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
I seem to struggle with (for me) complex recursive functions. Could anyone point me in the right direction so that I can simplify this messy and repetitive code? Also, it's meant to work for n-level deep.
$hasProducts = array();
function filterOutEmpty($levelZero) {
global $hasProducts;
foreach ($levelZero as $levelZeroK => $levelZeroV) {
if (empty($levelZeroV['products']) && empty($levelZeroV['families'])) continue;
if (!empty($levelZeroV['products'])) {
$hasProducts[$levelZeroK] = $levelZeroV;
}
if (!empty($levelZeroV['families'])) {
foreach ($levelZeroV['families'] as $levelOneK => $levelOneV) {
if (empty($levelOneV['products']) && empty($levelOneV['families'])) continue;
if (!empty($levelOneV['products'])) {
$hasProducts[$levelZeroK]['families'][$levelOneK] = $levelOneV;
}
if (!empty($levelOneV['families'])) {
foreach ($levelOneV['families'] as $levelTwoK => $levelTwoV) {
if (empty($levelTwoV['products']) && empty($levelTwoV['families'])) continue;
if (!empty($levelTwoV['products'])) {
$hasProducts[$levelZeroK]['families'][$levelOneK]['families'][$levelTwoK] = $levelTwoV;
}
}
}
}
}
}
}
Sample input could be as follows:
$districts = [
1 => [
'id' => 1,
'families' => [
0 => [
'id' => 2
],
1 => [
'id' => 3,
'families' => [
0 => [
'id' => 2,
'products' => [
1 => 'Arnold'
],
],
1 => [
'id' => 2,
'products' => [],
],
]
],
]
],
2 => [
'id' => 1,
'families' => [
0 => [
'id' => 2,
'products' => [
1 => 'John Doe'
],
],
1 => [
'id' => 3,
'products' => [],
],
]
],
3 => [
'id' => 1,
'products' => [
1 => 'Hi',
2 => 'Hello',
]
],
4 => [
'id' => 1,
'families' => [
0 => [
'id' => 2
],
1 => [
'id' => 3
],
]
],
];
The desired output:
(
[1] => Array
(
[families] => Array
(
[1] => Array
(
[families] => Array
(
[0] => Array
(
[id] => 2
[products] => Array
(
[1] => Arnold
)
)
)
)
)
)
[2] => Array
(
[families] => Array
(
[0] => Array
(
[id] => 2
[products] => Array
(
[1] => John Doe
)
)
)
)
[3] => Array
(
[id] => 1
[products] => Array
(
[1] => Hi
[2] => Hello
)
)
)
Please, keep in mind, that the array can be nested x-levels deep, this is just as an example. With each level I would need to add another if check (!empty($level{SOME_LEVEL}V['families'])) { ... the code again for a particular level... }
This code uses a recursive function to parse each level, so the number of levels doesn't matter. Each time it enters the routine it gets passed in the current level and will return the representation of that level in the new format ( I think ).
The one part is that after each recursive call, it checks if the return value is empty and only adds it in if it contains something...
function filterOutEmpty($levelZero) {
$hasProducts = [];
foreach ($levelZero as $levelZeroK => $levelZeroV) {
if (!empty($levelZeroV['products'])) {
$hasProducts[$levelZeroK] = $levelZeroV;
}
else if ( is_array($levelZeroV)) {
$new = filterOutEmpty($levelZeroV);
if ( !empty($new) ) {
$hasProducts[$levelZeroK] = $new;
}
}
if (!empty($levelZeroV['families'])) {
$new = filterOutEmpty($levelZeroV['families']);
if ( !empty($new) ) {
$hasProducts[$levelZeroK]['families'] = $new;
}
}
}
return $hasProducts;
}
print_r(filterOutEmpty($districts));
I have an associative array key/values indicating a group. I'd like to shuffle the array so that the groups are in a random order but so that the items in the groups are shuffled only within their group. In other words, I want to take something like this:
[
[
"name" => "Buffy",
"group" => 1
],
[
"name" => "Willow",
"group" => 1
],
[
"name" => "Xander",
"group" => 2
],
[
"name" => "Giles",
"group" => 2
],
[
"name" => "Tara",
"group" => 3
],
[
"name" => "Angel",
"group" => 3
],
[
"name" => "Spike",
"group" => 3
]
]
And return something a bit more like this:
[
[
"name" => "Spike",
"group" => 3
]
[
"name" => "Angel",
"group" => 3
],
[
"name" => "Tara",
"group" => 3
],
[
"name" => "Willow",
"group" => 1
],
[
"name" => "Buffy",
"group" => 1
],
[
"name" => "Xander",
"group" => 2
],
[
"name" => "Giles",
"group" => 2
],
]
I realise this is probably possible with two or three sorts, but it would be great if this could be done with a single usort.
Basically, my solution shuffles the input array, temporarily restructures the input array to gather subarrays with the same group value, then returns the data to its original structure.
Code: (Demo)
shuffle($data); // randomize all subarrays
foreach ($data as $set) {
$grouped[$set['group']][] = $set; // merge subarrays on group value
}
$output = [];
foreach($grouped as $group) {
array_push($output, ...$group); // return to original array structure
}
var_export($output);
*Note that ... (splat operator) allows array_push() to store all subarrays (within each group) individually to generate the original structure.
Possible Output:
array (
0 =>
array (
'name' => 'Xander',
'group' => 2,
),
1 =>
array (
'name' => 'Giles',
'group' => 2,
),
2 =>
array (
'name' => 'Willow',
'group' => 1,
),
3 =>
array (
'name' => 'Buffy',
'group' => 1,
),
4 =>
array (
'name' => 'Spike',
'group' => 3,
),
5 =>
array (
'name' => 'Tara',
'group' => 3,
),
6 =>
array (
'name' => 'Angel',
'group' => 3,
),
)
Try...
$groups = [];
foreach (array_unique(array_column($myar, 'group')) as $k) $groups[$k] = rand();
foreach (array_keys($myar) as $k) $myar[$k]['rnd'] = rand();
usort($myar, function($a, $b) use ($groups) {
if ($gdif = $groups[$b['group']] - $groups[$a['group']]) return $gdif;
return $b['rnd'] - $a['rnd'];
});
foreach (array_keys($myar) as $k) unset($myar[$k]['rnd']);
print_r($myar);
Run with your data, this is one result...
Array
(
[0] => Array
(
[name] => Buffy
[group] => 1
)
[1] => Array
(
[name] => Willow
[group] => 1
)
[2] => Array
(
[name] => Angel
[group] => 3
)
[3] => Array
(
[name] => Tara
[group] => 3
)
[4] => Array
(
[name] => Spike
[group] => 3
)
[5] => Array
(
[name] => Giles
[group] => 2
)
[6] => Array
(
[name] => Xander
[group] => 2
)
)