Please let me know how can sort array with one column line_total descending format. I want that array in line_total descending format bcoz i want to display data upper sale to lower sale. So please help me.
Array
(
[totalusers] => 1
[TranReport] => Array
(
[start] => 01-01-2017
[end] => 31-12-2017
)
[webhits] => 794
[paypal] => Yes
[cash] => No
[Transactions] => Array
(
[0] => Array
(
[order_date] => 03-02-2017
[customer_name] => Mohsin khan
[payment_method] => PayPal
[product_list] => Array
(
[0] => Array
(
[product_name] => USB Cable – Iphone → 1M USB Cable - Iphone
[qty] => 1
[line_total] => 9
)
[1] => Array
(
[product_name] => USB Cable – Iphone → 2M USB Cable - Iphone
[qty] => 2
[line_total] => 24
)
)
[quantity] => 3
[order_currency] => USD
[order_total] => 48.00$
[new_total] => 48.00
)
[1] => Array
(
[order_date] => 09-01-2017
[customer_name] => Mohsin khan
[payment_method] => PayPal
[product_list] => Array
(
[0] => Array
(
[product_name] => AA USB Charger
[qty] => 1
[line_total] => 15
)
[1] => Array
(
[product_name] => Car Charger - Dual USB - Low Profile
[qty] => 1
[line_total] => 15
)
[2] => Array
(
[product_name] => Mister Hose → 20m Mister Hose
[qty] => 1
[line_total] => 20
)
)
[quantity] => 3
[order_currency] => USD
[order_total] => 50.00$
[new_total] => 50.00
)
[2] => Array
(
[order_date] => 07-01-2017
[customer_name] => Mohsin khan
[payment_method] => PayPal
[product_list] => Array
(
[0] => Array
(
[product_name] => Quick Charge V3 - Dual USB - Car Charger
[qty] => 1
[line_total] => 15
)
[1] => Array
(
[product_name] => Car Charger - Dual USB - Low Profile
[qty] => 1
[line_total] => 15
)
[2] => Array
(
[product_name] => Mister Hose → 20m Mister Hose
[qty] => 1
[line_total] => 20
)
)
[quantity] => 1
[order_currency] => USD
[order_total] => 15.00$
[new_total] => 15.00
)
)
[deliveytotal] => 0
)
You need to use usort function http://php.net/manual/en/function.uksort.php . The rest is just my interpretation of what you want to achieve - sort transactions by sum of line_total in products
$array = []; //your array
function sumLinesTotal($product_list) {
return array_sum(array_map($product_list, function($product){
return $product['line_total'];
});
}
usort($array['Transactions'], function ($a, $b) {
return sumLinesTotal($a) < sumLinesTotal($b);
});
Related
I am working on a project with magento 2 (Not my specialty,still learning). I have my custom theme that I have applied and that inherits the Luma theme. I am trying to modify the product page styles and I need some product data to be shown. I can get that data however some of the data gives option id and I do not know how to get that option id's data. Hope that makes sense. Below is my phtml code that I used for debugging purpose just to see what data there is.
<div>
<pre><?= print_r($block->getProduct('material')->debug()) ?></pre>
</div>
The output it gives me is
Array
(
[store_id] => 1
[entity_id] => 38
[attribute_set_id] => 13
[type_id] => simple
[sku] => 24-WG084
[has_options] => 0
[required_options] => 0
[created_at] => 2022-07-25 07:40:05
[updated_at] => 2022-07-25 07:40:05
[name] => Sprite Foam Yoga Brick
[image] => /l/u/luma-yoga-brick.jpg
[small_image] => /l/u/luma-yoga-brick.jpg
[thumbnail] => /l/u/luma-yoga-brick.jpg
[options_container] => container2
[msrp_display_actual_price_type] => 0
[url_key] => sprite-foam-yoga-brick
[status] => 1
[visibility] => 4
[tax_class_id] => 2
[price] => 5.000000
[description] =>
Our top-selling yoga prop, the 4-inch, high-quality Sprite Foam Yoga Brick is popular among yoga novices and studio professionals alike. An essential yoga accessory, the yoga brick is a critical tool for finding balance and alignment in many common yoga poses. Choose from 5 color options.
Standard Large Size: 4" x 6" x 9".
Beveled edges for ideal contour grip.
Durable and soft, scratch-proof foam.
Individually wrapped.
Ten color choices.
[activity] => 15,16,24,18
[material] => 49
[gender] => 80,81,84
[category_gear] => 87
[options] => Array
(
)
[media_gallery] => Array
(
[images] => Array
(
[48] => Array
(
[value_id] => 48
[file] => /l/u/luma-yoga-brick.jpg
[media_type] => image
[entity_id] => 38
[label] => Image
[position] => 1
[disabled] => 0
[label_default] => Image
[position_default] => 1
[disabled_default] => 0
)
)
[values] => Array
(
)
)
[tier_price] => Array
(
)
[tier_price_changed] => 0
[quantity_and_stock_status] => Array
(
[is_in_stock] => 1
[qty] => 100
)
[category_ids] => Array
(
[0] => 10
[1] => 12
)
[is_salable] => 1
[website_ids] => Array
(
[0] => 1
)
[category (Magento\Catalog\Model\Category\Interceptor)] => Array
(
[entity_id] => 12
[attribute_set_id] => 3
[parent_id] => 10
[created_at] => 2022-07-25 07:40:01
[updated_at] => 2022-07-25 07:40:01
[path] => 1/2/10/12
[position] => 2
[level] => 3
[children_count] => 0
[name] => Fitness Equipment
[url_key] => fitness-equipment
[url_path] => gear/fitness-equipment
[is_active] => 1
[is_anchor] => 1
[include_in_menu] => 1
[path_ids] => Array
(
[0] => 1
[1] => 2
[2] => 10
[3] => 12
)
)
[category_id] => 12
[request_path] => sprite-foam-yoga-brick.html
[_cache_instance_store_filter (Magento\Store\Model\Store\Interceptor)] => Array
(
[store_id] => 1
[code] => default
[website_id] => 1
[group_id] => 1
[name] => Default Store View
[sort_order] => 0
[is_active] => 1
[available_currency_codes] => Array
(
[0] => USD
[1] => EUR
)
[base_currency (Magento\Directory\Model\Currency)] => Array
(
[currency_code] => USD
)
[current_currency (Magento\Directory\Model\Currency)] => Array
(
[currency_code] => USD
)
)
)
From this output, I need product dimensions, color and material. I can see dimension in product description and I can extract it from there. However, I cannot find colors of products, Perhaps that has to do with what type of product it is. I can also see material in the output and it goes like,
[material] => 49
That 49 seems to be option id. How can I get material data using this option id?
I have an array as below. The atrributes are order_id, qty, sell_price, product_name and merchant_name for every array. I want to map based on the same order_id and change the attributes become to array if the same order_id greater than 1.
[items:protected] => Array
(
[0] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 45000.00
[product_name] => Omni Bars Cokelat Kelapa
[merchant_name] => Health & Co.
)
[1] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 55000.00
[product_name] => Trim Eats Strawberry Banana Cream Overnight Oats
[merchant_name] => Trim Eats
)
[2] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 28000.00
[product_name] => Everything Banana Keripik Pisang Rumput Laut
[merchant_name] => EverythingBanana
)
[3] => stdClass Object
(
[order_id] => 2
[qty] => 1
[sell_price] => 35000.00
[product_name] => Naked Bar Banana Nutter
[merchant_name] => Naked Foodss
)
[4] => stdClass Object
(
[order_id] => 3
[qty] => 1
[sell_price] => 200000.00
[product_name] => Health & Co. Paket Detoks 1 Hari
[merchant_name] => Health & Co.
)
[5] => stdClass Object
(
[order_id] => 3
[qty] => 1
[sell_price] => 38000.00
[product_name] => Health & Co. Paket 3 Botol Holy Kale
[merchant_name] => Health & Co.
)
)
I want to map the array to something like this with the key is order_id.
[1] => stdClass Object
(
[qty] => [1,1,1]
[sell_price] => [45000.00, 55000.00, 28000.00]
[product_name] => [Omni Bars Cokelat Kelapa, Trim Eats Strawberry Banana Cream Overnight Oats, Everything Banana Keripik Pisang Rumput Laut]
[merchant_name] => [Health & Co, Trim Eats, EverythingBanana]
)
[2] => so on....
I stuck on it. Please help me.
Look this solution:
//$data - your array
$result = [];
foreach ($data as $item) {
$result[$item['order_id']]['qty'][] = $item['qty'];
$result[$item['order_id']]['sell_price'][] = $item['sell_price'];
$result[$item['order_id']]['product_name'][] = $item['product_name'];
$result[$item['order_id']]['merchant_name'][] = $item['merchant_name'];
}
// $result your result array
hope you are fine today.
I have an xml file online that I stored it into an array using file_get_contents(), simplexml_load_string() json_encode and json_decode; using the above methods gave an array like this:
[data] => Array
(
[transaction] => Array
(
[0] => Array
(
[fields] => Array
(
[transactionid] => 90397725
[transactionreference] => 90397725
[transactiontime] => 14:34:56
[transactiondate] => 2016-04-08
[upgauthcode] => 649192
[cardnumber] => ***************5104
[cardholdersname] => MISS Jane K Doe
[switchnumber] => Array
(
)
[cardstartyear] => 15
[cardstartmonth] => 02
[cardexpireyear] => 20
[cardexpiremonth] => 02
[transactionamount] => 108.00
[cardtype] => VISADEBIT
[baskettype] => order
[regisalu] => Miss
[regifnam] => Jane
[regilnam] => Doe
[regiadd1] => 14 Test Street
[regiadd4] => Test
[regiadd5] => West Test
[regiadd6] => fg28 5nZ
[regiadd7] => United Kingdom
[regidnum] => 07464000000
[regienum] => 07464000000
[regimobi] => 07464000000
[maddsalu] => Miss
[maddfnam] => Jane
[maddlnam] => Doe
[maddadd1] => 14 Test Street
[maddadd4] => Test
[maddadd5] => West Test
[maddadd6] => fg28 5nZ
[maddadd7] => United Kingdom
[madddnum] => 07464000000
[maddenum] => 07464000000
[maddidel] => Deliver
[baskettotal] => 90.00
[hirewaivertotal] => 0.00
[deliverytotal] => 0.00
[collectiontotal] => 0.00
[cardholderaddr1] => 14 Test Street
[cardholderaddr2] => Array
(
)
[cardholdercity] => Test
[cardholderstate] => West Test
[cardholderpostcode] => fg28 5nZ
[cardholdercountry] => United Kingdom
[regiuser] => jane.doe#gmail.com
[regihtml] => 0
[regigrde] => 0
[regigrdn] => 0
[regipopt] => 0
[maddcoll] => 1
[maddgrde] => 0
[maddgrdn] => 0
)
[orderitems] => Array
(
[item] => Array
(
[code] => DL008
[sku] => Array
(
)
[desc] => 22M LED Festoon (230v)
[description] => DL008, 22M LED Festoon (230v)
[price] => 45.00
[qty] => 2
[totalprice] => 90.00
)
)
)
[45] => Array
(
[fields] => Array
(
[transactionid] => 93645131
[transactionreference] => 93645131
[transactiontime] => 14:15:16
[transactiondate] => 2016-07-07
[upgauthcode] => 085526
[cardnumber] => ***************0103
[cardholdersname] => John Doe
[switchnumber] => Array
(
)
[cardstartyear] => 15
[cardstartmonth] => 12
[cardexpireyear] => 18
[cardexpiremonth] => 11
[transactionamount] => 588.00
[cardtype] => MASTERCARD
[baskettype] => order
[regisalu] => Mr
[regifnam] => John
[regilnam] => Doe
[regiadd1] => 39-45 TEST SQUARE
[regiadd2] => CITY Test Aquarium
[regiadd4] => London
[regiadd6] => bb2A 1op
[regiadd7] => United Kingdom
[regidnum] => 074640000000
[regienum] => 074640000000
[regimobi] => 07464000000
[maddsalu] => Mr
[maddfnam] => John
[maddlnam] => Doe
[maddadd1] => 39-45 TEST SQUARE
[maddadd2] => CITY Test AQUARIUM
[maddadd4] => London
[maddadd6] => bb2A 1op
[maddadd7] => United Kingdom
[madddnum] => 07464000000
[maddenum] => 07464000000
[maddidel] => Deliver
[baskettotal] => 490.00
[hirewaivertotal] => 0.00
[deliverytotal] => 0.00
[collectiontotal] => 0.00
[cardholderaddr1] => 39-45 TEST SQUARE
[cardholderaddr2] => CITY TEST AQUARIUM
[cardholdercity] => London
[cardholderpostcode] => bb2A 1op
[cardholdercountry] => United Kingdom
[regiuser] => john.doe#bloomberg.net
[regihtml] => 0
[regipopt] => 1
[regigrde] => 0
[regigrdn] => 0
[maddcoll] => 1
[maddgrde] => 0
[maddgrdn] => 0
)
[orderitems] => Array
(
[item] => Array
(
[0] => Array
(
[code] => PK289
[sku] => Array
(
)
[desc] => EvoLite Helmet STD Peak (white)
[description] => PK289, EvoLite Helmet STD Peak (white)
[price] => 9.00
[qty] => 6
[totalprice] => 54.00
)
[1] => Array
(
[code] => GGE3
[sku] => Array
(
)
[desc] => Graft Gear Safety Spectacle
[description] => GGE3, Graft Gear Safety Spectacle
[price] => 2.00
[qty] => 6
[totalprice] => 12.00
)
[2] => Array
(
[code] => GG025
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (M)
[description] => GG025, Graft Gear Hi-Vis Vest (M)
[price] => 5.00
[qty] => 2
[totalprice] => 10.00
)
[3] => Array
(
[code] => GG027
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (XL)
[description] => GG027, Graft Gear Hi-Vis Vest (XL)
[price] => 5.00
[qty] => 4
[totalprice] => 20.00
)
[4] => Array
(
[code] => GG029
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (3XL)
[description] => GG029, Graft Gear Hi-Vis Vest (3XL)
[price] => 5.00
[qty] => 4
[totalprice] => 20.00
)
[5] => Array
(
[code] => GG030
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (4XL)
[description] => GG030, Graft Gear Hi-Vis Vest (4XL)
[price] => 5.00
[qty] => 2
[totalprice] => 10.00
)
[6] => Array
(
[code] => GG037
[sku] => Array
(
)
[desc] => Graft Gear Bomber (M)
[description] => GG037, Graft Gear Bomber (M)
[price] => 23.00
[qty] => 1
[totalprice] => 23.00
)
[7] => Array
(
[code] => GG039
[sku] => Array
(
)
[desc] => Graft Gear Bomber (XL)
[description] => GG039, Graft Gear Bomber (XL)
[price] => 23.00
[qty] => 2
[totalprice] => 46.00
)
[8] => Array
(
[code] => GG041
[sku] => Array
(
)
[desc] => Graft Gear Bomber (XXXL)
[description] => GG041, Graft Gear Bomber (XXXL)
[price] => 23.00
[qty] => 3
[totalprice] => 69.00
)
[9] => Array
(
[code] => GG073
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG073, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 1
[totalprice] => 39.00
)
[10] => Array
(
[code] => GG071
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG071, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 2
[totalprice] => 78.00
)
[11] => Array
(
[code] => GG070
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG070, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 2
[totalprice] => 78.00
)
[12] => Array
(
[code] => PK454
[sku] => Array
(
)
[desc] => Ladies Black Hiker Boot
[description] => PK454, Ladies Black Hiker Boot
[price] => 31.00
[qty] => 1
[totalprice] => 31.00
)
)
)
)
)
)
)
Now I want to loop through that array, as I want to re-convert it into my XML format, I am able to access the first level of the array and the orderitems -> item -> code where the client purchased one single item, but I am not able to loop inside deeper level where the client purchased more than one item (second array)
here is my code
foreach($array['data']['transaction'] as $transaction){
//echo $transaction['fields']['transactionid']."<br>";
//echo $transaction['orderitems']['item']['code']."<br>";
if(isset($transaction['orderitems']['item']['code'])){
echo $transaction['orderitems']['item']['code']."<br>";
} else {
foreach($transaction['orderitems']['item'] as $multi_item){
echo "<--- MULTI ---><br>";
//tried so many things and none has worked;
//like $multi_item['code'];
}
}
}
how to access the code of each item purchased by john doe?
You can access this data by filtering the array.
In PHP >= 5.5.0 you can make a search like this;
$key = array_search('John Doe', array_column($yourArray, 'cardholdersname'));
This method will return the array that matched given criteria.
http://php.net/manual/tr/function.array-search.php
I am trying to fetch data using the following link:- https://dev.fitbit.com/docs/activity/ and API :- GET https://api.fitbit.com/1/user/[user-id]/activities/date/[date].json and getting response array as below
Array(
[result] => Array
(
[activities] => Array
(
[0] => Array
(
[activityId] => 90013
[activityParentId] => 90013
[activityParentName] => Walk
[calories] => 25
[description] => Walking less than 2 mph, strolling very slowly
[distance] => 0.271426
[duration] => 355000
[hasStartTime] => 1
[isFavorite] =>
[lastModified] => 2016-11-29T19:11:42.000Z
[logId] => 5007419020
[name] => Walk
[startDate] => 2016-11-30
[startTime] => 00:34
[steps] => 342
)
)
[goals] => Array
(
[activeMinutes] => 30
[caloriesOut] => 2157
[distance] => 8.05
[floors] => 10
[steps] => 10000
)
[summary] => Array
(
[activeScore] => -1
[activityCalories] => 55
[caloriesBMR] => 1257
[caloriesOut] => 1300
[distances] => Array
(
[0] => Array
(
[activity] => Walk
[distance] => 0.271426
)
[1] => Array
(
[activity] => total
[distance] => 0.48
)
[2] => Array
(
[activity] => tracker
[distance] => 0.42
)
[3] => Array
(
[activity] => loggedActivities
[distance] => 0.271426
)
[4] => Array
(
[activity] => veryActive
[distance] => 0.09
)
[5] => Array
(
[activity] => moderatelyActive
[distance] => 0.11
)
[6] => Array
(
[activity] => lightlyActive
[distance] => 0.16
)
[7] => Array
(
[activity] => sedentaryActive
[distance] => 0.12
)
)
[elevation] => 0
[fairlyActiveMinutes] => 5
[floors] => 0
[heartRateZones] => Array
(
[0] => Array
(
[caloriesOut] => 37.9099
[max] => 98
[min] => 30
[minutes] => 25
[name] => Out of Range
)
[1] => Array
(
[caloriesOut] => 33.193
[max] => 137
[min] => 98
[minutes] => 9
[name] => Fat Burn
)
[2] => Array
(
[caloriesOut] => 0
[max] => 166
[min] => 137
[minutes] => 0
[name] => Cardio
)
[3] => Array
(
[caloriesOut] => 0
[max] => 220
[min] => 166
[minutes] => 0
[name] => Peak
)
)
[lightlyActiveMinutes] => 6
[marginalCalories] => 33
[sedentaryMinutes] => 1426
[steps] => 789
[veryActiveMinutes] => 3
)
)
[code] => 200
[content_type] => application/json;charset=UTF-8
)
but the problem is this array just provides me the data of the activity that has been done using Fitbit application. But I have to fetch complete data whether its by the Fitbit application or Fitbit device i.e blaze.
Just glancing at the API, I believe you need to use activities/tracker/. The API states that this represents the daily activity values logged by the tracker device only, excluding manual activity log entries. So you just need to add tracker to your GET.
Tracker Activities:
activities/tracker/calories
activities/tracker/steps
activities/tracker/distance
activities/tracker/floors
activities/tracker/elevation
activities/tracker/minutesSedentary
activities/tracker/minutesLightlyActive
activities/tracker/minutesFairlyActive
activities/tracker/minutesVeryActive
activities/tracker/activityCalories
I have this array,
Array
(
[0] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[1] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
[2] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[3] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[4] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
[5] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
And I want to split it into two, so I get this result,
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[1] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[2] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
[1] => Array
(
[0] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[1] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
[2] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
)
)
Any ideas?
The tricky of this is that the total price of each package must not be more than 250. while the total weight of each must be as even as possbile (even thought it is impossible). You can see what I mean in this picture,
name weight price
package 1
Item 1 200 10
Item 3 300 30
Item 7 10 200
(total) 510 240
package 2
Item 4 500 20
Item 6 10 40
Item 2 20 100
(total) 530 160
So is it possible to move around the items between these two packages so that total weights are distributed 'evenly'? It seems that it cannot be achieved without 'moving' them manually. I can't think of any way to achieve the goal by automatic programme. Can you?
Sort the array by weight
usort($array, function($a, $b) {return $a['weight']>$b['weight'];});
We init our result tables, with a price and weight counters
$sum=Array(
Array("weight" => 0, "price" => 0, "res" => Array()),
Array("weight" => 0, "price" => 0, "res" => Array()));
Then we loop on array
while ($obj=array_pop($array)) {
We select the lower result array
$ind=($sum[0]['weight']<$sum[1]['weight']) ? 0 : 1;
except if it has reached the max amount
if ($sum[$ind]['price']+$obj['price']>250) {
$ind=1-$ind;
## object will be skipped if it can't fit in the allowed amount
if ($sum[$ind]['price']+$obj['price']>250) continue;
}
and then we fill it
array_push($sum[$ind]['res'], $obj);
$sum[$ind]['weight']+=$obj['weight'];
$sum[$ind]['price']+=$obj['price'];
}
print_r($sum);
Result as expected:
Array
(
[0] => Array
(
[weight] => 510
[price] => 240
[res] => Array
(
[0] => Array
(
[id] => 3
[name] => Item 3
[price] => 30
[weight] => 300
)
[1] => Array
(
[id] => 1
[name] => Item 1
[price] => 10
[weight] => 200
)
[2] => Array
(
[id] => 7
[name] => Item 7
[price] => 200
[weight] => 10
)
)
)
[1] => Array
(
[weight] => 530
[price] => 160
[res] => Array
(
[0] => Array
(
[id] => 4
[name] => Item 4
[price] => 20
[weight] => 500
)
[1] => Array
(
[id] => 2
[name] => Item 2
[price] => 100
[weight] => 20
)
[2] => Array
(
[id] => 6
[name] => Item 6
[price] => 40
[weight] => 10
)
)
)
)
It have nothing to do with PHP or arrays. As I understand your question it's kind of Multiple knapsack problem. So it's lots of math (see some solution here) and maybe easiest way is to programmatically chaeck all possible variants.