Sort array by mutiple fields (closest to number) - php

How can I sort a array by two(one) different values?
So I have a array like this:
array(
array(
'id' => 10,
'total' => 38,
'entry' => 400
),
array(
'id' => 4,
'total' => 34,
'entry' => 3100
),
array(
'id' => 2,
'total' => 34,
'entry' => 3150
),
array(
'id' => 8,
'total' => 34,
'entry' => 2980
),
);
The array is already sorted by the key total, but they all have the same value in total. So I need to sort by who is closest to 3000 by entry.
Edit
The array should be first sorted by total and then entry, since entry is only there so I can differentiate who is the best.
So the array should look like this:
array(
array(
'id' => 10,
'total' => 38,
'entry' => 400
),
array(
'id' => 8,
'total' => 34,
'entry' => 2980
),
array(
'id' => 4,
'total' => 34,
'entry' => 3100
),
array(
'id' => 2,
'total' => 34,
'entry' => 3150
)
);

Try this:
usort($arr, function ($a, $b) {
if ($a['total'] == $b['total']) { // Only compare on entry when the totals are the same.
return abs($a['entry'] - 3000) > abs($b['entry'] - 3000);
}
return $a['total'] < $b['total'];
});
print_r($arr);
Output:
Array
(
[0] => Array
(
[id] => 2
[total] => 35
[entry] => 3150
)
[1] => Array
(
[id] => 8
[total] => 34
[entry] => 2980
)
[2] => Array
(
[id] => 4
[total] => 34
[entry] => 3100
)
[3] => Array
(
[id] => 6
[total] => 34
[entry] => 3250
)
[4] => Array
(
[id] => 3
[total] => 32
[entry] => 3400
)
)
Here's how it works: it compares the totals, but if they're the same, it compares the absolute value of the difference between entry and 3000 of both entrys.
eval.in demo

Related

Remove array keys that aren't listed in a second array

The following is my result array
Array (
[0] => Array
(
[ProductID] => 220
[TextID] => 477
[ProductName] => Hugo Woman
[Price] => 43.91
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg
[CategoryID] => 1
[SubCategoryID] => 1
[View] => 0
)
[1] => Array
(
[ProductID] => 616
[TextID] => 959
[ProductName] => Hugo XY
[Price] => 44.95
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
[2] => Array
(
[ProductID] => 650
[TextID] => 991
[ProductName] => Hugo Just Different
[Price] => 45.76
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
)
I have a second array with subcategories, in which the key is referencing to the SubCategoryID:
Array
(
[1] => Array
(
[EN] => Ladies
[NL] => Dames
)
[2] => Array
(
[EN] => Men
[NL] => Heren
)
)
I want to loop through the result array and remove the keys who don't have a SubCategoryID listed in the second array. I looked at http://php.net/manual/en/function.array-filter.php, but can't figure out the best way to do this.
Thank you!
There are two solutions to the above problem, one with using simple for loop and one with using array_walk() function.
Here, $result_array is your result array and $subcategory_array is your subcategory array.
Solution(1):
$subcategory_ids = array_keys($subcategory_array);
$arrLength = count($result_array);
for($i = 0; $i < $arrLength; ++$i){
if(!in_array($result_array[$i]['SubCategoryID'], $subcategory_ids)){
unset($result_array[$i]);
}
}
// display $result_array
echo "<pre>"; print_r($result_array);
Solution(2):
$subcategory_ids = array_keys($subcategory_array);
function filter_arr($item, $key){
global $result_array, $subcategory_ids;
if(!in_array($item['SubCategoryID'], $subcategory_ids)){
unset($result_array[$key]);
}
}
array_walk($result_array, "filter_arr");
// display $result_array
echo "<pre>"; print_r($result_array);
Please try i thnik this help to you..
$array = array (
0 => array
(
'ProductID' => 220,
'TextID' => 477,
'ProductName' => 'Hugo Woman',
'Price' => 43.91,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg',
'CategoryID' => 1,
'SubCategoryID' => 1,
'View' => 0
),
1 => array
(
'ProductID' => 616,
'TextID' => 959,
'ProductName' => 'Hugo XY',
'Price' => 44.95,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg',
'CategoryID' => 1,
'SubCategoryID' => 2,
'View' => 0
),
'2' => array
(
'ProductID' => 650,
'TextID' => 991,
'ProductName' => 'Hugo Just Different',
'Price' => 45.76,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg',
'CategoryID' => 2,
'SubCategoryID' => 1,
'View' => 0
),);
$array1 = array (
1 => array
(
'EN' => 'Ladies',
'NL' => 'Dames'
),
2 => array
(
'EN' => 'Men',
'NL' => 'Heren'
),);
foreach($array as $newArray){
if (array_key_exists($newArray['SubCategoryID'], $array1)) {
echo '<pre>';
print_r($newArray);
echo '</pre>';}}

Array_map when elements are shuffled

I have this array (it's just a part of it). 6 = question ID, optionIDs = possible answers.
Array
(
[3] => Array
(
[0] => 6
[1] => Array
(
[0] => Array
(
[optionID] => 16
[isCorrect] => 0
)
[1] => Array
(
[optionID] => 14
[isCorrect] => 1
)
[2] => Array
(
[optionID] => 15
[isCorrect] => 0
)
[3] => Array
(
[optionID] => 17
[isCorrect] => 0
)
)
)
[7] => Array
(
[0] => 6
[1] => Array
(
[0] => Array
(
[optionID] => 16
[isCorrect] => 0
)
[1] => Array
(
[optionID] => 15
[isCorrect] => 0
)
[2] => Array
(
[optionID] => 17
[isCorrect] => 0
)
[3] => Array
(
[optionID] => 14
[isCorrect] => 1
)
)
)
)
I'm trying to merge redundant questions (6 and 6) with array_map:
$unique = array_map('unserialize', array_unique(array_map('serialize', $quizQuestionArray)));
And it works as long as optionIDs are in the same order. But in some cases (like here) they are shuffled (16,14,15,17) (16,15,17,14). Is there a way to keep them shuffled and remove duplicate questions?
array_map-serialize is a pretty crude way to deduplicate an array. You should be using something like this instead:
$dupeIds = [];
$array = array_filter($array, function ($item) use (&$dupeIds) {
$keep = !isset($dupeIds[$item[0]]);
$dupeIds[$item[0]] = true;
return $keep;
});
You will need to sort them to the same order before applying you array_map() function. You can use the uasort() function and supply your own comparison function like this:
// Example array
$array = array(
3 => array(
0 => 6,
1 => array(
0 => array(
'optionID' => 16,
'isCorrect' => 0
),
1 => array(
'optionID' => 14,
'isCorrect' => 1
),
2 => array(
'optionID' => 15,
'isCorrect' => 0
),
3 => array(
'optionID' => 17,
'isCorrect' => 0
),
)
),
7 => array(
0 => 6,
1 => array(
0 => array(
'optionID' => 16,
'isCorrect' => 0
),
1 => array(
'optionID' => 15,
'isCorrect' => 0
),
2 => array(
'optionID' => 17,
'isCorrect' => 0
),
3 => array(
'optionID' => 14,
'isCorrect' => 1
),
)
)
);
// You can supply parts of an array to uasort()
// uasort() will modify your array but keep your keys.
uasort($array[3][2], 'sort_by_optionid');
uasort($array[7][3], 'sort_by_optionid');
function sort_by_optionid($a, $b) {
if ($a['optionID'] === $b['optionID']) {
return 0;
} else if ($a['optionID'] > $b['optionID']) {
return 1;
} else {
return -1;
}
}
// Done.
Now the keys are preserved and you can easily array_map() to find the duplicates and then sort again back to the original state according to the keys. E.g. with uksort()

Looping through array of unlimited children and display to a table

I don't have any idea how to loop through this array and display it to an HTML table. Child data are dynamic and can be unlimited. Anyone can give me a clue? Thanks!
(
[0] => Array
(
[Discount] => Array
(
[id] => 8
[parent_id] => 0
[lft] => 1
[rght] => 6
[name] => Discount 1
[value] => 25
)
[children] => Array
(
[0] => Array
(
[Discount] => Array
(
[id] => 10
[parent_id] => 8
[lft] => 2
[rght] => 5
[name] => Child of D1
[value] => 32
)
[children] => Array
(
[0] => Array
(
[Discount] => Array
(
[id] => 11
[parent_id] => 10
[lft] => 3
[rght] => 4
[name] => The 1.1.1
[value] => 65
)
[children] => Array
(
)
)
)
)
)
)
)
I know I can't just do:
foreach($discounts as $discount){
echo "<div>{$discount['Discount']['name']}</div>";
}
Try this
foreach ($Discount as $children => $value) {
echo "<div>{$discount['Discount']['$children']['name']}</div>";
}
You will need recursion to properly generate the HTML. Here is an example that I hope is helpful. This isn't exactly HOW you'll want to generate the final HTML but this should get you started on the right track:
$data = array(
0 => array(
'discount' => array(
'id' => 8,
'parent_id' => 0,
'lft' => 1,
'rght' => 6,
'name' => 'Discount 1',
'value' => 25,
),
'children' => array(
0 => array(
'discount' => array(
'id' => 10,
'parent_id' => 8,
'lft' => 2,
'rght' => 5,
'name' => 'Child of D1',
'value' => 32,
),
'children' => array(
0 => array(
'discount' => array(
'id' => 11,
'parent_id' => 10,
'lft' => 3,
'rght' => 4,
'name' => 'The 1.1.1',
'value' => 65,
),
'children' => array(),
)
)
)
)
)
);
$html = getHtml($data);
/**
* This function is called recursively to generate the necessary HTML
* #param array $data
*/
function getHtml(array $data)
{
// This is your base conditon to end the recursion. It stops once it hits an empty children array
if (empty($data)) {
return;
}
$data = $data[0]; // will have to tweak this if you have more than 1 element per array
$html = sprintf('<div id="%d" name="%s">', $data['discount']['id'], $data['discount']['name']);
$html .= getHtml($data['children']); // this recursive call generates inner HTML
$html .= '</div>';
return $html;
}
// See the result
var_dump($html);

Get multiple arrays from multidimensional array based on common IDs

I have one large array and I want to group this into other arrays based on a common ID so that I can use array_splice to get only the first and last occurrence of that ID.
array(
[0] => array(id => 34, name = "walter"),
[1] => array(id => 25, name = "walter jr"),
[2] => array(id => 34, name = "saul"),
[3] => array(id => 25, name = "jesse"),
[4] => array(id => 25, name = "todd")
)
What I want to end up with is something like this.
array(
[0] => array(
id => 34, name = "walter",
id => 34, name = "saul"
),
[1] => array(
id => 25, name = "walter jr",
id => 25, name = "jesse",
id => 25, name = "todd"
)
)
I'm having a really hard time trying to wrap my head around how to accomplish this and have searched all over. I've found some solutions using array_unique and array_diff but i'm never able to get the result i'm looking for.
You can use array_reduce to group array elements, see below:
$data = array(
0 => array('id' => 34, 'name' => "walter"),
1 => array('id' => 25, 'name' => "walter jr"),
2 => array('id' => 34, 'name' => "saul"),
3 => array('id' => 25, 'name' => "jesse"),
4 => array('id' => 25, 'name' => "todd")
);
$result = array_reduce($data, function ($result, $item){
if (!isset($result[$item['id']])) {
$result[$item['id']] = array();
}
$result[$item['id']][] = $item;
return $result;
}, array());
print_r(array_values($result));
and result is:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 34
[name] => walter
)
[1] => Array
(
[id] => 34
[name] => saul
)
)
[1] => Array
(
[0] => Array
(
[id] => 25
[name] => walter jr
)
[1] => Array
(
[id] => 25
[name] => jesse
)
[2] => Array
(
[id] => 25
[name] => todd
)
)
)
If you want the key 0,1,2,3,4 ... just go through the entire array and overwrite the keys.
But depending on what you are doing, use foreach to traverse the array without the key. =p
$all_array = array(
0 => array( 'id' => 34 , 'name' => "walter" ) ,
1 => array( 'id' => 25 , 'name' => "walter jr" ) ,
2 => array( 'id' => 34 , 'name' => "saul" ) ,
3 => array( 'id' => 25 , 'name' => "jesse" ) ,
4 => array( 'id' => 25 , 'name' => "todd" )
) ;
$array_sort = array() ;
foreach ( $all_array as $piece_array ) {
$array_sort[$piece_array['id']][] = $piece_array ;
}
ksort( $array_sort ) ;
var_dump( $array_sort ) ;

Have the following array merged

I have the following array:
Array
(
[0] => Array
(
[Vendor_ID] => 1
[Quantity] => 55
)
[1] => Array
(
[Vendor_ID] => 1
[Quantity] => 55
)
[2] => Array
(
[Vendor_ID] => 1
[Quantity] => 55
)
[3] => Array
(
[Vendor_ID] => 3
[Quantity] =>
)
[4] => Array
(
[Vendor_ID] => 3
[Quantity] =>
)
[5] => Array
(
[Vendor_ID] => 3
[Quantity] =>
)
[6] => Array
(
[Vendor_ID] => 4
[Quantity] =>
)
[7] => Array
(
[Vendor_ID] => 4
[Quantity] =>
)
[8] => Array
(
[Vendor_ID] => 4
[Quantity] =>
)
)
Which is being created with the following code:
$Display_Arr = array();
$Tick = 0;
foreach ($_POST['product'] AS $_1){
if (!in_array($_1['vendor_id'], $Display_Arr)){
$Display_Arr[$Tick] = array(
"Vendor_ID" => $_1['vendor_id'],
"Quantity" => ""
);
$Display_Arr[$Tick]["Quantity"] .= $_1['quantity'];
}else{
$Display_Arr[$Tick]["Quantity"] .= $_1['quantity'];
}
++$Tick;
}
echo "<pre>";
print_r($Display_Arr);
echo "</pre>";
But I am not getting my desired output, which is:
Array
(
[0] => Array
(
[Vendor_ID] => 1
[Quantity] => 55,55,55
)
[1] => Array
(
[Vendor_ID] => 3
[Quantity] =>
)
[2] => Array
(
[Vendor_ID] => 4
[Quantity] =>
)
)
Where am I going wrong with this?
#mathielo
The current output is:
Array
(
[1] => Array
(
[Vendor_ID] => 1
[Quantity] => 55
)
[3] => Array
(
[Vendor_ID] => 3
[Quantity] =>
)
[4] => Array
(
[Vendor_ID] => 4
[Quantity] =>
)
)
Whereas, i'm trying to obtain:
[0] => Array
(
[Vendor_ID] => 1
[Quantity] => 55,55,55
)
If I got it right, what you need is this:
EDIT: Just made some tests and got it right this time:
$Display_Arr = array();
foreach ($_POST['product'] AS $_1){
if (!array_key_exists($_1['Vendor_ID'], $Display_Arr)){
$Display_Arr[$_1['Vendor_ID']] = array(
"Vendor_ID" => $_1['Vendor_ID'],
"Quantity" => $_1['Quantity']
);
}else{
if(!empty($_1['Quantity']))
$Display_Arr[$_1['Vendor_ID']]["Quantity"] .= ",{$_1['Quantity']}";
}
}
echo "<pre>";
print_r($Display_Arr);
echo "</pre>";
The main problem was in if (!in_array($_1['vendor_id'], $Display_Arr)). PHP's in_array() checks for given needle in the array values, and we were trying to match to the Vendor_ID value stored in the outer array keys. That was fixed using array_key_exists().
EDIT 2: I used this for test data:
$_POST['product'] = array(
0 => array(
'Vendor_ID' => 1,
'Quantity' => 55
),
1 => array(
'Vendor_ID' => 1,
'Quantity' => 55
),
2 => array(
'Vendor_ID' => 1,
'Quantity' => 55
)
,
3 => array(
'Vendor_ID' => 3,
'Quantity' => ''
),
4 => array(
'Vendor_ID' => 3,
'Quantity' => ''
),
5 => array(
'Vendor_ID' => 3,
'Quantity' => ''
),
6 => array(
'Vendor_ID' => 4,
'Quantity' => ''
),
7 => array(
'Vendor_ID' => 4,
'Quantity' => ''
),
8 => array(
'Vendor_ID' => 4,
'Quantity' => ''
)
);
You won't be needing $Tick anymore, as you could use Vendor_ID as keys for the outer array.
Looks like the easiest way to solve this would be to first aggregate the vendor quantities, and then build the final array with the keys that you are using.
I am assuming the input looks something like this:
$_POST['product'] = [
['vendor_id' => 1, 'quantity' => 55],
['vendor_id' => 1, 'quantity' => 55],
['vendor_id' => 1, 'quantity' => 55],
['vendor_id' => 3, 'quantity' => null],
['vendor_id' => 3, 'quantity' => null],
['vendor_id' => 3, 'quantity' => null],
['vendor_id' => 4, 'quantity' => null],
['vendor_id' => 4, 'quantity' => null],
['vendor_id' => 4, 'quantity' => null],
];
Aggregating the quantities:
$aggregates = [];
foreach ($_POST['product'] as $product) {
$id = $product['vendor_id'];
if ( ! isset($aggregates[$id])) {
$aggregates[$id] = [];
}
if ($product['quantity'] > 0) {
$aggregates[$id][] = $product['quantity'];
}
}
The aggregates array should now look like this:
$aggregates = [
1 => [
0 => 55,
1 => 55,
2 => 55,
],
3 => [], // Empty
4 => [], // Empty
];
As you can see the data is now neatly organized and ready to be put into any format you want. Using the keys that you use in your question it is as simple as:
$output = [];
foreach ($aggregates as $vid => $qty) {
$quantity = implode(',', $qty);
$output[] = ['Vendor_ID' => $vid, 'Quantity' => $quantity];
}
The output should now look like this:
$output = [
['Vendor_ID' => 1, 'Quantity' => '55,55,55'],
['Vendor_ID' => 3, 'Quantity' => ''],
['Vendor_ID' => 4, 'Quantity' => ''],
];
This will output exactly what you are looking for although the output of the last answer (Sverri M. Olsen) is more useful. Here you get the quantities as a string while with Sverri's method you get an array in first place.
$Display_Arr = array();
$vendors=array();
foreach ($_POST['product'] AS $_1){
if (!in_array($_1['vendor_id'],$vendors)){
$vendors[]=$_1['vendor_id'];
$Display_Arr[sizeof($vendors)-1] = array(
"Vendor_ID" => $_1['vendor_id'],
"Quantity" => $_1['quantity']
);
}
else{
$vendorKey=array_search($_1['vendor_id'],$vendors);
$Display_Arr[$vendorKey]["Quantity"] .=(!empty($Display_Arr[$vendorKey]["Quantity"])?',':null).$_1['quantity'];
}
}

Categories