In my backend I have some code handling teams for a round robin group tournament, and in the end teams are returned in an array and contains match results/stats etc.
I have the following array which I want to sort by two fields in the following order: points (desc), round_difference (desc).
The final and correct order would be: TeamOne, TeamThree, TeamTwo.
How would I go on about this with an array in this format? Is it possible at all? I have looked into array_multisort() however my array keys/structure overall is a bit "odd" compared to the other code examples I found here on the page and was very confusing to me.
Hopefully someone can assist me in this.
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
)
Try uasort() instead and compare the keys like so:
uasort($arr[1],function($a,$b){
# If the points are more/less right off the bat, return results
if($a['matches']['points'] < $b['matches']['points'])
return true;
# If they are the same
elseif($a['matches']['points'] == $b['matches']['points'])
# Compare round_difference sort by that
return ($a['matches']['round_difference'] < $b['matches']['round_difference']);
});
So, sorting something like this:
$arr = array(
1 => array(
1 => array(
'id' => 1,
'group' => 1,
'name' => 'TeamOne',
'tag' => 'One',
'matches' => array(
'played' => 1,
'won' => 0,
'ot_won' => 1,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 3,
'points' => 2
)
),
238 => array
(
'id' => 238,
'group' => 1,
'name' => 'TeamTwo',
'tag' => 'Two',
'matches' => array(
'played' => 0,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 0,
'lost' => 0,
'round_difference' => 0,
'points' => 0,
)
),
14 => array
(
'id' => 14,
'group' => 1,
'name' => 'TeamThree',
'tag' => 'Three',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 1,
)
),
17 => array
(
'id' => 17,
'group' => 1,
'name' => 'TeamFive',
'tag' => 'Five',
'matches' => array
(
'played' => 1,
'won' => 0,
'ot_won' => 0,
'ot_lost' => 1,
'lost' => 0,
'round_difference' => '-3',
'points' => 2,
)
)
)
);
Gives you:
Array
(
[1] => Array
(
[1] => Array
(
[id] => 1
[group] => 1
[name] => TeamOne
[tag] => One
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 1
[ot_lost] => 0
[lost] => 0
[round_difference] => 3
[points] => 2
)
)
[17] => Array
(
[id] => 17
[group] => 1
[name] => TeamFive
[tag] => Five
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 2
)
)
[14] => Array
(
[id] => 14
[group] => 1
[name] => TeamThree
[tag] => Three
[matches] => Array
(
[played] => 1
[won] => 0
[ot_won] => 0
[ot_lost] => 1
[lost] => 0
[round_difference] => -3
[points] => 1
)
)
[238] => Array
(
[id] => 238
[group] => 1
[name] => TeamTwo
[tag] => Two
[matches] => Array
(
[played] => 0
[won] => 0
[ot_won] => 0
[ot_lost] => 0
[lost] => 0
[round_difference] => 0
[points] => 0
)
)
)
)
Related
I have this array structure.
I'm trying to remove those whole array if I found the [empevalpptwo] empty or null, so if the [empevalpptwo] is empty the whole [4] => Array should be remove.
Right now i'm just trying to get the parent index so I can just remove it by index
Is there any good solution like filtering recursively?
$kra = array_column($ppform_plan->toArray(), 'kra');
$emp_eval_pptwo = array_search('', array_column($kra, 'empevalpptwo'));
//should return 4
Array
(
[4] => Array
(
[id] => 50
[user_id] => 6282
[specific_user_id] => 6281
[eval_cat_id] => 2
[format_cat] => 1
[title] => This istesting
[desc] =>
[weight] => 50
[bsc_weight_group] =>
[bsc_rating] =>
[sequence] =>
[created_at] => 2019-05-22 10:55:23
[updated_at] => 2019-05-22 10:55:23
[kra] => Array
(
[0] => Array
(
[id] => 77
[user_id] => 6282
[bsc_id] => 50
[index] => 0
[kra_title] => ttes lang muna
[kra_desc] =>
[kra_weight] => 25
[sat] => 521
[at] => 4
[ot] => 535
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 5
[rank] => 1
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
[1] => Array
(
[id] => 78
[user_id] => 6282
[bsc_id] => 50
[index] => 1
[kra_title] => talga e2 pa o
[kra_desc] =>
[kra_weight] => 25
[sat] => 5
[at] => 2
[ot] => 4
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 4
[rank] => 2
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
)
)
)
Something akin to:
<?php
$items =
[
1 =>
[
'foo'=>
[
[
'bar' => '',
],
[
'bar' => '',
]
]
],
2 =>
[
'foo'=>
[
[
'bar' => 'baz'
]
]
]
];
foreach ($items as $k => $item)
if(empty(array_filter(array_column($item['foo'], 'bar'))))
unset($items[$k]);
var_export($items);
Output:
array (
2 =>
array (
'foo' =>
array (
0 =>
array (
'bar' => 'baz',
),
),
),
)
You should try this :
$data = array_map('array_filter', $data);
$data = array_filter($data);
UPDATED
and for checking inner values you can use this :
$array = array_filter($array, function($v) { return !empty($v['empevalpptwo']); });
Orginal
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
[personalinfo] =>
)
)
Result :
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
)
)
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I want to sort array by value P if two P are same then sort by PD if two PD is same then sort by PF same as PA.
How i achieve this?
I have Multidimensional array In PHP Like
PHP:
$abc = array(
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '2','L' => '0','D'=> '0','PF' => '2904','PA' => '1932','PD' => '972','P' => '6')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '2320','PA' => '1974','PD' => '346','P' => '3')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '1620','PA' => '1824','PD' => '-204','P' => '3')),
array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '0','L' => '2','D'=> '0','PF' => '2200','PA' => '2300','PD' => '-100','P' => '0'))
);
HTML :
Array
(
[0] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 0
[L] => 2
[D] => 0
[PF] => 2200
[PA] => 2300
[PD] => -100
[P] => 0
)
)
[1] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 2320
[PA] => 1974
[PD] => 346
[P] => 3
)
)
[2] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 1620
[PA] => 1824
[PD] => -204
[P] => 3
)
)
[3] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 2
[L] => 0
[D] => 0
[PF] => 2904
[PA] => 1932
[PD] => 972
[P] => 6
)
)
)
Want similar like this.
Array
(
[0] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 2
[L] => 0
[D] => 0
[PF] => 2904
[PA] => 1932
[PD] => 972
[P] => 6
)
)
[1] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 2320
[PA] => 1974
[PD] => 346
[P] => 3
)
)
[2] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 1
[L] => 1
[D] => 0
[PF] => 1620
[PA] => 1824
[PD] => -204
[P] => 3
)
)
[3] => Array
(
[id] => 1
[name] => abc
[overall] => Array
(
[MP] => 2
[W] => 0
[L] => 2
[D] => 0
[PF] => 2200
[PA] => 2300
[PD] => -100
[P] => 0
)
)
)
I have tried like this.
$memberArray1 = usort($memberArray, function($a, $b) {
return $b['overall']['P'] - $a['overall']['P'];
});
$memberArray1 = usort($memberArray, function($a, $b) {
return $b['overall']['PD'] - $a['overall']['PD'];
});
But the issue is when i sort like P then it works but after when i sort like PD then it sort by PD not like P.
You can use uasort along with rsort as
uasort($abc,function($a,$b){
$c = $a['P'] - $b['P'];
$c .= $a['PD'] - $b['PD'];
$c .= $a['PF'] - $b['PF'];
$c .= $a['PA'] - $b['PA'];
return $c;
});
array_reverse($abc);
print_r($abc);
Demo
hi this is the part of result of print_r($this->Product) :
[stock_item] => Mage_CatalogInventory_Model_Stock_Item Object
(
[_minSaleQtyCache:Mage_CatalogInventory_Model_Stock_Item:private] => Array
(
)
[_qtyIncrements:protected] =>
[_eventPrefix:protected] => cataloginventory_stock_item
[_eventObject:protected] => item
[_productInstance:protected] => Mage_Catalog_Model_Product Object
*RECURSION*
[_customerGroupId:protected] =>
[_processIndexEvents:protected] => 1
[_resourceName:protected] => cataloginventory/stock_item
[_resource:protected] =>
[_resourceCollectionName:protected] => cataloginventory/stock_item_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[item_id] => 3843
[product_id] => 2573
[stock_id] => 1
[qty] => 2
[min_qty] => 0.0000
[use_config_min_qty] => 1
[is_qty_decimal] => 0
[backorders] => 0
[use_config_backorders] => 1
[min_sale_qty] => 1.0000
[use_config_min_sale_qty] => 1
[max_sale_qty] => 0.0000
[use_config_max_sale_qty] => 1
[is_in_stock] => 1
[low_stock_date] =>
[notify_stock_qty] =>
[use_config_notify_stock_qty] => 1
[manage_stock] => 0
[use_config_manage_stock] => 1
[stock_status_changed_auto] => 0
[use_config_qty_increments] => 1
[qty_increments] => 0.0000
[use_config_enable_qty_inc] => 1
[enable_qty_increments] => 0
[is_decimal_divided] => 0
[type_id] => grouped
[stock_status_changed_automatically] => 0
[use_config_enable_qty_increments] => 1
[product_name] => Amino 12500
[store_id] => 1
[product_type_id] => grouped
[product_status_changed] => 1
[product_changed_websites] =>
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[item_id] => 3843
[product_id] => 2573
[stock_id] => 1
[qty] => 0.0000
[min_qty] => 0.0000
[use_config_min_qty] => 1
[is_qty_decimal] => 0
[backorders] => 0
[use_config_backorders] => 1
[min_sale_qty] => 1.0000
[use_config_min_sale_qty] => 1
[max_sale_qty] => 0.0000
[use_config_max_sale_qty] => 1
[is_in_stock] => 1
[low_stock_date] =>
[notify_stock_qty] =>
[use_config_notify_stock_qty] => 1
[manage_stock] => 0
[use_config_manage_stock] => 1
[stock_status_changed_auto] => 0
[use_config_qty_increments] => 1
[qty_increments] => 0.0000
[use_config_enable_qty_inc] => 1
[enable_qty_increments] => 0
[is_decimal_divided] => 0
[type_id] => grouped
[stock_status_changed_automatically] => 0
[use_config_enable_qty_increments] => 1
)
[_idFieldName:protected] => item_id
[_isDeleted:protected] =>
[_oldFiel
dsMap:protected] => Array
(
[stock_status_changed_automatically] => stock_status_changed_auto
[use_config_enable_qty_increments] => use_config_enable_qty_inc
)
[_syncFieldsMap:protected] => Array
(
[stock_status_changed_automatically] => stock_status_changed_auto
[use_config_enable_qty_increments] => use_config_enable_qty_inc
[stock_status_changed_auto] => stock_status_changed_automatically
[use_config_enable_qty_inc] => use_config_enable_qty_increments
)
)
As you can see there is 2 ['qty'] values . If I type :
$this->Product['stock_item']['qty'] = 2; I can access the first ['qty']. My question is how can i access the second ['qty'] ? Thx
Magento provides magic getters and setters for anything "data", so you could do this to get and set 'qty':
$qty = $this->product->getQty();
$this->product->setQty($qty);
But you can also use these methods to get the arrays:
$dataArray = $this->product->getData();
$origDataArray = $this->product->getOrigData();
$this->product->setData($dataArray);
$this->product->setOrigData($origDataArray);
Or this to get/set a specific value:
$dataQty = $product->getData('qty');
$origDataQty = $product->getOrigData('qty');
$product->setData('qty', $dataQty);
$product->setOrigData('qty', $origDataQty);
This is the cart array in the Virtuemart 2.0.22a ..
this is stored in "$cart"
how to print [virtuemart_product_id] => 21 at 4.
$cart=
VirtueMartCart Object
(
[products] => Array
(
[21] => stdClass Object
(
[virtuemart_manufacturer_id] => Array
(
)
[slug] => stylish-shirt
[published] => 1
[virtuemart_product_price] =>
[virtuemart_product_id] => 21
[virtuemart_shoppergroup_id] =>
[product_price] => 450.00000
[override] => 0
[product_override_price] => 0.00000
[product_tax_id] => -1
[product_discount_id] => -1
[product_currency] => 68
[virtuemart_vendor_id] => 1
[product_parent_id] => 0
[product_sku] =>
[product_name] => Stylish Shirt
[product_s_desc] =>
[product_weight] =>
[product_weight_uom] => KG
[product_length] =>
[product_width] =>
[product_height] =>
[product_lwh_uom] => M
[product_in_stock] => 0
[product_ordered] => 0
[product_sales] => 0
[product_unit] => KG
[product_packaging] =>
[min_order_level] =>
[max_order_level] =>
[virtuemart_media_id] => Array
(
[0] => 21
)
[step_order_level] =>
[image] => VmImage Object
(
[media_attributes] => 0
[setRole] =>
[file_name] => 3
[file_extension] => jpg
[virtuemart_media_id] => 21
[_foldersToTest:VmMediaHandler:private] => Array
(
[0] => E:\wamp\www\ecomm\images\stories\virtuemart\product\
[1] => E:\wamp\www\ecomm\images\stories\virtuemart\product\resized\
)
[_actions:VmMediaHandler:private] => Array
(
)
[_mLocation:VmMediaHandler:private] => Array
(
)
[_hidden:VmMediaHandler:private] => Array
(
)
[theme_url] => http://localhost/ecomm/components/com_virtuemart/
[virtuemart_vendor_id] => 1
[file_title] => 3.jpg
[file_description] =>
[file_meta] =>
[file_mimetype] => image/jpeg
[file_type] => product
[file_url] => images/stories/virtuemart/product/3.jpg
[file_url_thumb] => images/stories/virtuemart/product/resized/3_90x90.jpg
[published] => 1
[file_is_downloadable] => 0
[file_is_forSale] => 0
[file_is_product_image] => 0
[shared] => 0
[file_params] =>
[file_lang] =>
[_translatable] =>
[_tablePreFix] =>
[created_on] => 2013-08-12 12:32:19
[created_by] => 572
[modified_on] => 2013-08-12 12:38:50
[modified_by] => 572
[file_url_folder] => images/stories/virtuemart/product/
[file_path_folder] => images\stories\virtuemart\product\
[file_url_folder_thumb] => images/stories/virtuemart/product/resized/
[media_role] => file_is_displayable
[file_name_thumb] => 3_90x90
)
[categories] => Array
(
[0] => 6
)
[virtuemart_category_id] => 6
[category_name] => Shirts
[link] => /ecomm/index.php/component/virtuemart/shirts/stylish-shirt-detail?Itemid=0
[packaging] =>
[quantity] => 1
)
)
[_inCheckOut] =>
[_dataValidated] =>
[_blockConfirm] =>
[_confirmDone] =>
[_redirect] =>
[_redirect_disabled] =>
[_lastError] =>
[vendorId] => 1
[lastVisitedCategoryId] => 0
[virtuemart_shipmentmethod_id] => 0
[virtuemart_paymentmethod_id] => 1
[automaticSelectedShipment] => 1
[automaticSelectedPayment] => 1
[BT] => 0
[ST] => 0
[tosAccepted] =>
[customer_comment] =>
[couponCode] =>
[order_language] =>
[cartData] => Array
(
[VatTax] => Array
(
)
[duty] => 1
[payment] => 0
[paymentName] =>
Finally i got the output.. after lot of struggles
foreach($cart->products as $cur)
{
echo $cur->virtuemart_product_id;
}
I found this peice of php which prints out an array from the vm session:
<?php print_r(unserialize($_SESSION['__vm']['vmcart'])); ?>
thiss is the array which is printed out:
stdClass Object
(
[products] => Array
(
[1] => stdClass Object
(
[virtuemart_manufacturer_id] => Array
(
)
[slug] => basic
[published] => 1
[virtuemart_product_price_id] => 1
[virtuemart_product_id] => 1
[virtuemart_shoppergroup_id] =>
[product_price] => 10.00000
[override] =>
[product_override_price] => 0.00000
[product_tax_id] => 0
[product_discount_id] => 0
[product_currency] => 52
[virtuemart_vendor_id] => 1
[product_parent_id] => 0
[product_sku] => 001
[product_name] => Basic
[product_s_desc] = d
[product_weight] => 0.0000
[product_weight_uom] => KG
[product_length] => 0.0000
[product_width] => 0.0000
[product_height] => 0.0000
[product_lwh_uom] => M
[product_in_stock] => 0
[product_ordered] => 2
[product_sales] => 0
[product_unit] => KG
[product_packaging] => 0.0000
[min_order_level] =>
[max_order_level] =>
[virtuemart_media_id] => Array
(
)
[categories] => Array
(
[0] => 1
)
[virtuemart_category_id] => 1
[category_name] => Policies
[link] => /locksure-upgrade/index.php/our-policies/policies/basic-detail
[packaging] => 0
[quantity] => 2
[amount] => 2
[url] => /locksure-upgrade/index.php/our-policies/policies/basic-detail
[customfields] =>
[cart_item_id] => 1
)
)
[vendorId] => 1
[lastVisitedCategoryId] => 0
[virtuemart_shipmentmethod_id] => 1
[virtuemart_paymentmethod_id] => 1
[automaticSelectedShipment] => 1
[automaticSelectedPayment] => 1
[order_number] =>
[BT] => Array
(
[company] =>
[email] => sean#rmspr.co.uk
[title] =>
[first_name] => lock
[middle_name] =>
[last_name] => sure
[address_1] => 20 market street
[address_2] =>
[zip] => wa141pf
[city] => altrincham
[virtuemart_country_id] => 222
[virtuemart_state_id] => 65
[phone_1] => 01619273131
[phone_2] =>
[fax] =>
)
[ST] => 0
[tosAccepted] =>
[customer_comment] =>
[couponCode] =>
[cartData] => Array
(
[duty] => 1
[payment] => 0
[paymentName] => <span class="vmpayment_name">payment</span>
[totalProduct] => 1
[DBTaxRulesBill] => Array
(
)
[taxRulesBill] => Array
(
)
[DATaxRulesBill] => Array
(
)
[shipmentName] => <span class="vmshipment_name">shipment</span>
)
[lists] => Array
(
[shipTo] =>
<input name="shipto" id="shipto1" value="1" checked="checked" type="radio">
<label for="shipto1" id="shipto1-lbl" class="radiobtn">- Default (Same as Billing)<br></label>
[billTo] => 1
)
[pricesUnformatted] => Array
(
[basePrice] => 20
[basePriceWithTax] => 0
[discountedPriceWithoutTax] => 0
[salesPrice] => 20
[taxAmount] => 0
[salesPriceWithDiscount] => 0
[discountAmount] => 0
[priceWithoutTax] => 20
[subTotalProducts] => 0
[1] => Array
(
[costPrice] => 10.00000
[basePrice] => 10
[basePriceVariant] => 10
[basePriceWithTax] => 0
[discountedPriceWithoutTax] => 0
[priceBeforeTax] => 10
[salesPrice] => 10
[taxAmount] => 0
[salesPriceWithDiscount] => 0
[salesPriceTemp] => 10
[unitPrice] => 0
[discountAmount] => 0
[priceWithoutTax] => 10
[variantModification] => 0
[DBTax] => Array
(
)
[Tax] => Array
(
)
[VatTax] => Array
(
)
[DATax] => Array
(
)
[subtotal_with_tax] => 20
[subtotal_tax_amount] => 0
[subtotal_discount] => 0
[subtotal] => 20
)
[discountBeforeTaxBill] => 0
[withTax] => 20
[discountAfterTax] => 20
[shipmentValue] => 0
[shipmentTax] => 0
[shipmentTotal] => 0
[salesPriceShipment] => 0
[shipment_calc_id] => 0
[cost] => 0
[paymentValue] => 0
[paymentTax] => 0
[paymentTotal] => 0
[salesPricePayment] => 0
[payment_calc_id] => 0
[billSub] => 20
[billDiscountAmount] => 0
[billTaxAmount] => 0
[billTotal] => 20
)
[pricesCurrency] => 52
[paymentCurrency] => 52
[_inCheckOut] => 1
[_dataValidated] =>
[_confirmDone] =>
[STsameAsBT] => 0
)
from that array how would i extract the amount of items in the cart? if its even possible?
Solved using the following code for anyone who needs it:
<?php $array = unserialize($_SESSION['__vm']['vmcart']);
$amount = $array->products[1]->amount;
if ($amount != 0){ echo $amount; } else { echo 0; } ?>