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
Related
I have 2 arrays like:
$arr1 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
)
$arr2 = [220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
I need the result like :
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
For this I simply using array_merge
$arr3= $arr1+ $arr2;
But after that I got the result like:
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
My issue is after merging 2 associative arrays with different keys, I got the first array and second array mixed which means I need the result like the first three array elements of the first array, after that 4 array elements of the second array.
Can you anyone help me, please, It will be helpful for me
You could use:
foreach($arr2 as $key => $record) {
$arr1[$key] = $record;
}
This would result in the order you're looking for.
demo
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);
});
please someone will guide me to how to add the values of below conditions and then update the added value in code .
how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total.
Array
(
[0] => Array
(
[order_total_id] => 999
[order_id] => 194
[code] => goods_total
[title] => Goods-Total
[text] => £130.00
[value] => 130.0000
[sort_order] => 1
)
[1] => Array
(
[order_total_id] => 1000
[order_id] => 194
[code] => low_order_fee
[title] => * Carriage
[text] => £10.00
[value] => 10.0000
[sort_order] => 3
)
[2] => Array
(
[order_total_id] => 1001
[order_id] => 194
[code] => sub_total
[title] => Sub-Total
[text] => £130.00
[value] => 130
[sort_order] => 4
)
[3] => Array
(
[order_total_id] => 1002
[order_id] => 194
[code] => tax
[title] => VAT (20%)
[text] => £26.00
[value] => 26.0000
[sort_order] => 5
)
[4] => Array
(
[order_total_id] => 1003
[order_id] => 194
[code] => total
[title] => Invoice Total
[text] => £166.00
[value] => 166.0000
[sort_order] => 9
)
)
A rudimentary solution:
foreach ($array_var as $key => $item) {
if ($item['code'] == 'low_order_fee') {
$first_val = $item['value'];
}
if ($item['goods_total'] == 'low_order_fee') {
$sec_val = $item['value'];
$position = $key;
}
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;
But maybe you should think about store the values in another way to make easier access to them.
I hope it helps.
I have an array in php called $json. It looks like this
Array (
[0] => Array (
[name] => Knife
[url] => http://steamcommunity.com/market/listings/753/297120...
[price] => 0.16 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 30
[game] => Counter-Strike: Global Offensive )
[1] => Array (
[name] => Strange Knife
[url] => http://steamcommunity.com/market/listings/440/Strange%2...
[price] => 0.55 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 177
[game] => Team Fortress 2 )
[2] => Array (
[name] => Festive Knife
[url] => http://steamcommunity.com/market/listings/440/Festive%20Knife" id="resultlink_2
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 66
[game] => Team Fortress 2 )
[3] => Array (
[name] => â… Flip Knife
[url] => http://steamcommunity.com/market/listings/7
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 24
[game] => Counter-Strike: Global Offensive )
...
How I can remove a part of an array where GAME != "Counter-Strike: Global Offensive" ?
$array; // Your array.
foreach($array as $subarray) {
if($subarray['game'] != "Counter-Strike: Global Offensive") {
unset($subarray);
}
}
I have a products table:
products
-id
-name
-image
And a product_atts table:
product_attributes
-product_id (FK)
-size (1, 2)
-memebership (1, 2, 3, 4, 6, 11)
-price
Now, when querying the product_attributes table and joining the products table, I get all of the variations:
Array
(
[0] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 1
[price] => 0.00
)
[1] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 2
[price] => 0.00
)
[2] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 3
[price] => 0.00
)
[3] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 4
[price] => 0.00
)
[4] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 6
[price] => 0.00
)
[5] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 1
[membership] => 11
[price] => 0.00
)
[6] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 1
[price] => 0.00
)
[7] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 2
[price] => 0.00
)
[8] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 3
[price] => 0.00
)
[9] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 4
[price] => 0.00
)
[10] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 6
[price] => 0.00
)
[11] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => 2
[membership] => 11
[price] => 0.00
)
)
Is there a way that I can get a result that looks like this:
Array
(
[0] => stdClass Object
(
[name] => Product 1
[image] => Product1.png
[size] => stdClass Object
(
[0] => 1,
[1] => 2
)
[membership] => stdClass Object
(
[0] => 1,
[1] => 2,
[2] => 3,
[3] => 4,
[4] => 6,
[5] => 11
)
[price] => 0.00
)
)
It makes it so much easier to then use this data in my UI, etc.
You might not already have realized it but using Eloquent, you already have everything this way. As soon as you create your relationships in your models, you just need a root model to reach every single one related to it.
Let's say that you have your Product model:
<?php
class Product extends Eloquent {
}
and your Products Details model:
<?php
class ProductDetail extends Eloquent {
}
So you can build a relation between them
class Product extends Eloquent {
public function details()
{
return $this->hasMany('ProductDetail');
}
}
And now if your controller pass to your UI a list of models:
public function index()
{
return View::make('site.products')->with('products', Product::all());
}
In your UI you'll be able to:
<ul>
#foreach($products as $product)
<li>
<p>Product: $product->name</p>
<p>Sizes:</p>
<ul>
#foreach($product->details as $detail)
<li>
{{$detail->size}}
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>