group same name from array and show total of an indivisual group - php

I have an array which look like this:
Array
(
[0] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 500
[amount] => 50000
)
[1] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 0
[product_ordered_quantity] => 45
[others_feature] =>
[rate] => 45
[amount] => 2025
)
[2] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 2
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 850
[amount] => 85000
)
.......
Now I want to group the same product (product will be identified by combination of products_name, measurement and products_type) to show one product at a time and make total of "product_ordered_quantity" of the same product. See the image you may understand what I am trying to say. I will generate PDF file so no javascript please.
result:
I want:

If this is a query from the DB you can use some methods provided by the Collection class (https://laravel.com/docs/5.4/eloquent-collections)
$query; //The query object BEFORE getting the data, so before get() or paginate()
$data = $query->selectRaw('sum(product_ordered_quantity) as quantity')
->groupBy('products_name','measurement', 'products_type')
->get();

You can give a try to this beautiful class, which will let you query to your array same as you query to the database - https://phplinq.codeplex.com/

Related

json_encode loses some object values in php

I have an object $myObject; I'm trying to return php object as json, but it loses some data. This is how $myObject looks:
CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList Object
(
[priceListId:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 32
[amounts:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 1000
[1] => 2000
[2] => 3000
[3] => 4000
[4] => 5000
[5] => 6000
[6] => 7000
)
[amountsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[1000] => 0
[2000] => 1
[3000] => 2
[4000] => 3
[5000] => 4
[6000] => 5
[7000] => 6
)
[periods:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
[4] => 30
)
[periodsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[10] => 0
[15] => 1
[20] => 2
[25] => 3
[30] => 4
)
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 7000
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 30
[prices:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[30] => Array
(
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
)
)
[settings:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings Object
(
[mode:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => credits
[preset:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] =>
[implementation:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => matrix
[defaults:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => Array
(
[period] =>
[amount] =>
)
)
)
After json_encode($myObject); new data looks like this (screenshot for better json view):
screenshot of returned json
Why information I wrote below is missing and how to access it?
Missing stuff:
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
The problem is json_encode can not access private properties.
Here are some workarounds you could do:
Convert the properties to be public
You could use a reflection class to convert the properties accessibility before you convert it
Example:
$refObject = new ReflectionObject( $obj );
$refProperty = $refObject->getProperty( 'property' );
$refProperty->setAccessible( true );
You could loop through the properties of the object using a Reflection object and call all the getters converting the object into an array structure that could be easily translated
Refelection Class Documentation

how to get the first galley image value in multidimensional array

Here i having one multidimensional using this array productId wise i am making one more array,from this array i having Galery array, from this i have to take first prodcutImage value, i don't how can achive this, if any one update my asnwer
I having array like this
print_r($productByCode)
Array
(
[0] => Array
(
[productId] => 5
[adminproductId] => 1Delivery00123
[categoryName] => Computer Accessories
[brandName] => Dell
[title] => Dell Inspiron 5000 Series 5547 5555 5557 5558 Internal Laptop Keyboard (Black)
[subTitle] => Dell Keyboard
[price] => 200
[discountType] => Percentage
[discountValue] => 20
[afterDiscount] => 160
[shipppingAmount] => 150
[taxPercentage] => 25
[Galery] => Array
(
[0] => Array
(
[gId] => 13
[productId] => 5
[prodcutImage] => 1.jpeg
)
[1] => Array
(
[gId] => 14
[productId] => 5
[prodcutImage] => 2.jpeg
)
[2] => Array
(
[gId] => 15
[productId] => 5
[prodcutImage] => 3.jpeg
)
)
)
)
I have tried like this
$itemArray = array
(
$productByCode[0]["productId"]=>array
(
'categoryName'=>$productByCode[0]["categoryName"],
'productId'=>$productByCode[0]["productId"],
'brandName'=>$productByCode[0]["brandName"],
'subTitle'=>$productByCode[0]["subTitle"],
'quantity'=>$Qty_total,
'price'=>$productByCode[0]["afterDiscount"],
'taxPercentage'=>$productByCode[0]["taxPercentage"].'%',
'shipppingAmount'=>$productByCode[0]["shipppingAmount"],
'qtyprice'=>round($productByCode[0]["afterDiscount"])
)
);
Current Output
Array
(
[5] => Array
(
[categoryName] => Computer Accessories
[productId] => 5
[brandName] => Dell
[subTitle] => Dell Keyboard
[quantity] => 1
[price] => 160
[taxPercentage] => 25%
[shipppingAmount] => 150
[qtyprice] => 160
)
)
Expected Output
Array
(
[5] => Array
(
[categoryName] => Computer Accessories
[productId] => 5
[brandName] => Dell
[subTitle] => Dell Keyboard
[quantity] => 1
[price] => 160
[taxPercentage] => 25%
[shipppingAmount] => 150
[qtyprice] => 160
[prodcutImage] => 1.jpeg
)
)
If I understood you requirement, You've already tried like this way to assign values to different array element, So just grab the Galery value like this using nested array element accessing way & add this element at the last of your array.
'prodcutImage' => $productByCode[0]['Galery'][0]['prodcutImage']
So it will like this,
$itemArray = array
(
$productByCode[0]["productId"]=>array
(
'categoryName'=>$productByCode[0]["categoryName"],
'productId'=>$productByCode[0]["productId"],
'brandName'=>$productByCode[0]["brandName"],
'subTitle'=>$productByCode[0]["subTitle"],
'quantity'=>$Qty_total,
'price'=>$productByCode[0]["afterDiscount"],
'taxPercentage'=>$productByCode[0]["taxPercentage"].'%',
'shipppingAmount'=>$productByCode[0]["shipppingAmount"],
'qtyprice'=>round($productByCode[0]["afterDiscount"]),
'prodcutImage' => $productByCode[0]['Galery'][0]['prodcutImage']
)
);

how to get single object value from array in magento?

i am trying to calculate shipping charge and i am getting a response from REST API. i want to get return the value of "delivery_charges" form response array?
Array
(
[response] => Array
(
[delivery_charges] => 41
[return_charges] => 83
[all_charges] => Array
(
[DL] => 32
[FS] => 30
[DTO] => 0
[RTO] => 32
[ST] => 14.5
[COD] => 0
[TDS] => 0
[CARD] => 0
[CNC] => 0
)
[canc_charges] => 0
[pickup_charges] => 0
[params] => Array
(
[md] => E
[pt] => prepaid
[cl] => xxxxxxx
[o_pin] => 560076
[d_pin] => 560062
[token] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[gm] => 100
[d_city] => Bangalore
[o_city] => Bangalore
)
)
)
i got answer by just using
return $shipping_charge['response']['delivery_charges'];
You need to simply add the following at the end. If $shipping_charge is the array then write:
return $shipping_charge['response']['delivery_charges'];

Array in php codeigniter framework

I am having problem with the array order problem, array coming from the database is correct but when i do print_r($data) each and every time i refresh the pages the order of the array change.
Array
(
[0] => stdClass Object
(
[prodtmappid] => 53624
[totalrating] => 5.00
[SKUNO] => C46P53624R7621
[ProId] => 31369
[CatId] => 46
[proQnty] => 15
[productMRP] => 1299
[ProDisPrice] => 0
[SellingPrice] => 390
[AdditionDiscount] => 0
[FinalPrice] => 390
[ProDiscount] => 70
[ProDisType] => percentage
[ProName] => Trendy Bandey Men TB20 Yellow Tshirt
[TotalValues] => 0.00
[image] => MCTSTB-TB20-YL_1_55x_885.JPG
[BrandId] => 4233
[FDisId] => 7
[ColorID] => 191
[size_Val] => M
)
[1] => stdClass Object
(
[prodtmappid] => 9526
[totalrating] => 0.00
[SKUNO] => C46P9526R1870
[ProId] => 3351
[CatId] => 46
[proQnty] => 8
[productMRP] => 1899
[ProDisPrice] => 0
[SellingPrice] => 1329
[AdditionDiscount] => 0
[FinalPrice] => 1329
[ProDiscount] => 30
[ProDisType] => percentage
[ProName] => FCUK 56DPJ Pink Men T-Shirt
[TotalValues] => 0.00
[image] => FCUK2_1_28x_885.JPG
[BrandId] => 1285
[FDisId] => 4
[ColorID] => 194
[size_Val] => S
)
so what problem i am facing is array[0]['prodtmappid'] will have diffrent values on every page refress.
What exactly is your query? From the results, it looks like it's ORDER BY prodQnty DESC and GROUP BY catId. If you want to keep the results consistent, add ORDER BY prodtmappid to your query.

Merge arrays from 2 SQL results

So i have a tags table setup in SQL
lists, lists_tags, tags
Each list can have multiple tags, and tags have two types genre and producer. Now if we are looking for all lists with tags 'action' these are the steps im following
SELECT GROUP_CONCAT(mini_lists_tags.list_id) AS list_ids
FROM (`mini_tags`)
LEFT JOIN `mini_lists_tags` ON `mini_lists_tags`.`tag_id` = `mini_tags`.`tag_id`
WHERE `mini_tags`.`tag_slug` = 'action'
That will return an array 1,2 for the ids of the list.
SELECT *
FROM (`mini_lists_anime`)
JOIN `mini_lists` ON `mini_lists`.`list_id` = `mini_lists_anime`.`list_id`
WHERE `mini_lists`.`list_id` IN ('1', '2')
AND `mini_lists`.`list_state` = 'active'
that gets all the lists in an array EXAMPLE:
Array
(
[0] => stdClass Object
(
[list_id] => 1
[list_episodes] => 13
[list_duration] => 24
[list_aired] => 1238623200
[list_age_rate] => PG-13 - Teens 13 or older
[user_id] => 1
[list_mal] => 5342
[list_category] => Anime
[list_type] => TV
[list_status] => Completed
[list_title] => Asura Cryin'
[list_alt_titles] => アスラクライン
[list_thumb] => 17071
[list_likes] => 0
[list_date] => 1300609723
[list_update] => 0
[list_state] => active
[list_info] =>
)
[1] => stdClass Object
(
[list_id] => 2
[list_episodes] => 26
[list_duration] => 23
[list_aired] => 1238623200
[list_age_rate] => PG-13 - Teens 13 or older
[user_id] => 1
[list_mal] => 329
[list_category] => Anime
[list_type] => TV
[list_status] => Completed
[list_title] => Planetes
[list_alt_titles] => プラネテス
[list_thumb] => 4822
[list_likes] => 0
[list_date] => 1300609723
[list_update] => 0
[list_state] => active
[list_info] =>
)
)
And then we get the tags
SELECT `mini_lists_tags`.`list_id`, `mini_tags`.`tag_type`, GROUP_CONCAT(mini_tags.tag_name) AS tag_names
FROM (`mini_lists_tags`)
INNER JOIN `mini_tags` ON `mini_tags`.`tag_id` = `mini_lists_tags`.`tag_id`
WHERE `mini_lists_tags`.`list_id` IN ('1', '2')
GROUP BY `mini_lists_tags`.`list_id`, `mini_tags`.`tag_type`
that gets all the tags in an array EXAMPLE:
Array
(
[0] => stdClass Object
(
[list_id] => 1
[tag_type] => Genre
[tag_names] => Supernatural,Action,Mecha
)
[1] => stdClass Object
(
[list_id] => 1
[tag_type] => Producers
[tag_names] => Seven Arcs
)
[2] => stdClass Object
(
[list_id] => 2
[tag_type] => Genre
[tag_names] => Romance,Action,Sci-fi,Comedy,Slice of Life,Drama,Space
)
[3] => stdClass Object
(
[list_id] => 2
[tag_type] => Producers
[tag_names] => Sunrise,Bandai Entertainment,Bandai Visual,Bang Zoom! Entertainment
)
)
Now the problem is I need to get them merged on the list_id so it returns something like this for each one.
stdClass Object
(
[list_id] => 1
[list_episodes] => 13
[list_duration] => 24
[list_aired] => 1238623200
[list_age_rate] => PG-13 - Teens 13 or older
[user_id] => 1
[list_mal] => 5342
[list_category] => Anime
[list_type] => TV
[list_status] => Completed
[list_title] => Asura Cryin'
[list_alt_titles] => アスラクライン
[list_thumb] => 17071
[list_likes] => 0
[list_date] => 1300609723
[list_update] => 0
[list_state] => active
[list_info] =>
[list_tags] => Array
(
[0] => stdClass Object
(
[tag_type] => Genre
[tag_names] => Mecha,Action,Supernatural
)
[1] => stdClass Object
(
[tag_type] => Producers
[tag_names] => Seven Arcs
)
)
)
Any advice is appreciated, i'm really lost. if there is better solution than this, i am all ears.
You can do another type of join that will return the parent item multiple times merged with each child object like so:
Array(
[0] => stdClass Object
(
[list_id] => 1
[list_episodes] => 13
[list_duration] => 24
...etc
[tag_type] => Genre
[tag_names] => Supernatural,Action,Mecha
...etc
)
[1] => stdClass Object
(
[list_id] => 1
[list_episodes] => 13
[list_duration] => 24
...etc
[tag_type] => Producers
[tag_names] => Seven Arcs
...etc
)
[2] => stdClass Object
(
[list_id] => 2
[list_episodes] => 26
[list_duration] => 23
...etc
[tag_type] => Genre
[tag_names] => Supernatural,Action,Mecha
...etc
)
[3] => stdClass Object
(
[list_id] => 2
[list_episodes] => 26
[list_duration] => 23
...etc
[tag_type] => Producers
[tag_names] => Seven Arcs
...etc
)
)
You will then need to loop through your results merging down the results into their child/parent relationships. This is because SQL always returns rows as results, not complex structures.
Although this is more complex to deal with, it's ususally less process intensive than making looped sql queries for each parent object (known as n+1 queries)

Categories