Laravel group collection and sort by the biggest value - laravel

I have a Laravel collection with values, which I want to group by 'type' and sort by the biggest 'probability'. Here is how my collection looks like:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[2] => Array
(
[type] => 3-way
[name] => 2
[probability] => 24.682032371379
[probability_odd] => 4.3553949845985
[prognose_type_id] => 1
[odd] => 2.66
[value_bet_diff] => -1.6953949845985
)
[3] => Array
(
[type] => 3-way
[name] => X
[probability] => 22.857984044926
[probability_odd] => 4.7029519221256
[prognose_type_id] => 1
[odd] => 3.7
[value_bet_diff] => -1.0029519221256
)
[4] => Array
(
[type] => double-chance
[name] => 1X
[probability] => 37.658793461327
[probability_odd] => 2.8545789739758
[prognose_type_id] => 4
[odd] => 1.49
[value_bet_diff] => -1.3645789739758
)
[5] => Array
(
[type] => double-chance
[name] => X2
[probability] => 23.770008208152
[probability_odd] => 4.5225058005294
[prognose_type_id] => 4
[odd] => 1.55
[value_bet_diff] => -2.9725058005294
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
I want to achieve as result something like that:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
Haven't been able to find similar solutions, so I finally decided to try here. Can you please help me?
Thanks in advance!

I think below can work;
$collection->sortByDesc('probability')->groupBy('type');
Sort by probability than group by type;

Probably better to do this in Eloquent, but as you have presented this as a collections problem;
assuming your collection is called $items
$sorted = $items->groupBy('type')
->map(function($group){
return $group->sortByDesc('probability')->take(1);
})
->flatten(1);

The most short:
$items = $collections
->sortByDesc('probability')
->unique('type');

Related

How to return array instead of object in laravel 5

I have use in model .
$data = DB::select('select * from users');
return $data;
But in controller I have got like this.
Array
(
[0] => stdClass Object
(
[Id] => 10
[Name] => Sachin
[Gender] => M
)
[1] => stdClass Object
(
[Id] => 12
[Name] => Sourav
[Gender] => M
)
)
But I want Like this
Array
(
[0] => Array
(
[Id] => 10
[Name] => Sachin
[Gender] => M
)
[1] => Array
(
[Id] => 12
[Name] => Sourav
[Gender] => M
)
)
I already tried using get() and toArray() but its giving call to unknown member function.
Anyone have an idea how to resolved this issue.
You just use the laravel standard function
public function testData(){
$data = YOUR_MODEL_NAME::all()->toArray();
echo "<pre>";print_r($data);
}
UPDATED:
public function testData(){
$data = DB::select('select * from users');
$data = collect($data)->map(function($x){ return (array) $x; })->toArray();
echo "<pre>";print_r($data);
}
Output will be :
UPDATED:
Array
(
[0] => Array
(
[email] => bmc-admin#gmail.com
[member_code] => BMC0001ADMIN
)
[1] => Array
(
[email] => bmc-analyst#gmail.com
[member_code] => BMC0003ADMIN
)
[2] => Array
(
[email] => bmc-product-manager#gmail.com
[member_code] => BMC0002ADMIN
)
)
Array
(
[0] => Array
(
[id] => 1
[drewry_user_id] => 1
[email] => bmc-admin#gmail.com
[name] => BMC Administrator
[mobile] =>
[member_name] => BMC-Admin
[member_code] => BMC0001ADMIN
[shipper_size] => Large
[fk_role_id] => 1
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-12 04:56:31
[updated_by] =>
[deleted_at] =>
)
[1] => Array
(
[id] => 6
[drewry_user_id] => 3
[email] => bmc-analyst#gmail.com
[name] => BMC Analyst
[mobile] =>
[member_name] => BMC-Analyst
[member_code] => BMC0003ADMIN
[shipper_size] => Large
[fk_role_id] => 3
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-26 10:55:41
[updated_by] =>
[deleted_at] =>
)
[2] => Array
(
[id] => 9
[drewry_user_id] => 2
[email] => bmc-product-manager#gmail.com
[name] => BMC Product Manager
[mobile] =>
[member_name] => BMC-Product-Manager
[member_code] => BMC0002ADMIN
[shipper_size] => Large
[fk_role_id] => 2
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-14 09:09:10
[updated_by] =>
[deleted_at] =>
)
[3] => Array
(
[id] => 19
[drewry_user_id] => 4
[email] => bmc-customer1#gmail.com
[name] => User 1
[mobile] =>
[member_name] => User-1
[member_code] => BMC0004CUSTOMER1
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-27 11:31:20
[updated_by] =>
[deleted_at] =>
)
[4] => Array
(
[id] => 20
[drewry_user_id] => 5
[email] => bmc-customer2#gmail.com
[name] => User 2
[mobile] =>
[member_name] => User-2
[member_code] => BMC0004CUSTOMER2
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:34:08
[updated_by] =>
[deleted_at] =>
)
[5] => Array
(
[id] => 21
[drewry_user_id] => 6
[email] => bmc-customer3#gmail.com
[name] => User 3
[mobile] =>
[member_name] => User-3
[member_code] => BMC0004CUSTOMER3
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:35:25
[updated_by] =>
[deleted_at] =>
)
[6] => Array
(
[id] => 22
[drewry_user_id] => 7
[email] => bmc-customer4#gmail.com
[name] => User 4
[mobile] =>
[member_name] => User-4
[member_code] => BMC0004CUSTOMER4
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-18 06:35:54
[updated_by] =>
[deleted_at] =>
)
[7] => Array
(
[id] => 23
[drewry_user_id] => 8
[email] => bmc-customer5#gmail.com
[name] => User 5
[mobile] =>
[member_name] => User-5
[member_code] => BMC0004CUSTOMER5
[shipper_size] => Large
[fk_role_id] => 4
[fk_country_id] =>
[timezone] => Asia/Kolkata
[status] => 1
[created_at] =>
[created_by] =>
[updated_at] => 2017-07-25 15:35:46
[updated_by] =>
[deleted_at] =>
)
)
I hope it will work to you.
If you just need to get raw nested arrays of data, without using Eloquent:
$data = DB::connection()->getPdo()
->query("SELECT * FROM users")
->fetchAll(\PDO::FETCH_ASSOC);
This should be much more efficient than the current accepted answer.
toArray is a model method of Eloquent, so you need to a Eloquent model, try this:
User::all()->toArray();
http://laravel.com/docs/eloquent#collections

How do I remove indexes in findall of cakephp

I have the following code:
$s= $this->ShowFare->find('all');
Array
(
[0] => Array
(
[ShowFare] => Array
(
[id] => 21
[press_release_id] => 14
[category] => 0
[title] => pr
[image_name] => 1486719733.jpg
[crop_image] => 1486719729.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 09:42:13
[created_by] => 0
[modified] => 2017-02-10 09:42:13
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] =>
[title] =>
)
)
[1] => Array
(
[ShowFare] => Array
(
[id] => 22
[press_release_id] => 7
[category] => 0
[title] => abcd
[image_name] => 1486721484.jpg
[crop_image] => 1486721481.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 10:11:24
[created_by] => 0
[modified] => 2017-02-10 10:11:24
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] => 7
[title] => ht48 hours, 04 March 2016
)
)
)
I want the above results in following format:
Array
(
Array
(
[ShowFare] => Array
(
[id] => 21
[press_release_id] => 14
[category] => 0
[title] => pr
[image_name] => 1486719733.jpg
[crop_image] => 1486719729.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 09:42:13
[created_by] => 0
[modified] => 2017-02-10 09:42:13
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] =>
[title] =>
)
)
Array
(
[ShowFare] => Array
(
[id] => 22
[press_release_id] => 7
[category] => 0
[title] => abcd
[image_name] => 1486721484.jpg
[crop_image] => 1486721481.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 10:11:24
[created_by] => 0
[modified] => 2017-02-10 10:11:24
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] => 7
[title] => ht48 hours, 04 March 2016
)
)
)
Is this possible?
Thanks in advance...

How to convert from newsoap to xml using simplexelement,simple_to_string

error_reporting(0);
session_start();
$link=$_POST['Host'];
$_SESSION['Host']=$link;
$client = new SoapClient("http://".$link."/api/soap/?wsdl");
/*api credentials*/
$apiuser=$_POST['User'];
$_SESSION['User']=$apiuser;
$apikey =$_POST['Key'];
$_SESSION['Key']=$apikey;
/*Action to be made */
$action =$_POST['Catalog'];
$_SESSION['Catalog']=$action;
/*Error trapping if in case number will be input*/
if(ctype_alpha(!$action))
{
die('Incorrect format');
}
else
{
$sess_id= $client->login($apiuser, $apikey);
/api login/
echo"Total Active list Product:".(count($client->call($sess_id, $action)));
echo"";
print_r($result=$client->call($sess_id,$action));
echo"";
}
?>
Array
(
[0] => Array
(
[product_id] => 1
[sku] => ROx12345
[name] => acer netbook
[set] => 4
[type] => simple
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)
[1] => Array
(
[product_id] => 2
[sku] => Cats1234
[name] => starbucks mug
[set] => 4
[type] => simple
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)
[2] => Array
(
[product_id] => 3
[sku] => samsung2141
[name] => samsung 3110
[set] => 4
[type] => grouped
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)

Iterating an array via Smarty in SugarCRM

When iterating an array in Smarty I am getting results conflicting with a print_r() of the same array.
When doing a print_r() on the array I can see that there are 2 objects in the array and each object is unique. However, when I run a print_r() on each item of the array (via foreach loop) I seem to get the same results for each item.
Can anyone identify what I'm doing wrong?
See the below code examples and results.
Code:
{$fields.rgggo_spreadsheetcellmap_documents.value->beans|#print_r}
Ouput:
Array
(
[a852b076-a5cb-dda9-3868-52010d6957ab] => RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => a852b076-a5cb-dda9-3868-52010d6957ab
[name] =>
[date_entered] => 08/06/2013 09:52am
[date_modified] => 08/06/2013 02:16pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] => Test 2
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => document_type_text_c
[row] => 3
[col] => 1
.... a bunch more stuff here
)
[f038918d-a13d-69f0-3541-51fff82f0497] => RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
)
When I iterate the array...
{{foreach from=$fields.rgggo_spreadsheetcellmap_documents.value->beans key=k item=v}}
<pre>
{$v|#print_r}
</pre>
{{/foreach}}
I get these results:
RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
I found the answer in another stackoverflow question. The issue is double vs single curly brackets and their use in SugarCRM.
Is there a difference between using a single vs. double brace (delimiters) in SugarCRM Smarty tpls command construct?

Get default value for attributes set to customer in Magento

I'm using an extension that switch customer groups based on some rules.
I added an attribute to customer as an EAV using this code.
$setup->addAttribute('customer', 'autoswitchgroup', array(
'type' => 'varchar',
'input' => 'select',
'label' => 'Autoswitch Group',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => 'yes',
'visible_on_front' => 1,
'source' => 'bitstream_all/entity_autoswitchgroup',
));
Looking over the extension code I see it gets the customer attribute using this code:
$customerAttributeValue = $this->_getCustomer()->getDataUsingMethod($attribute);
where _getCustomer() is defined as an Object. (#return Varien_Object).
If I take a look on the customer object I see this info:
[_resourceCollectionName:protected] => customer/customer_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[website_id] => 1
[email] => ****
[group_id] => 11
[increment_id] => 000000001
[store_id] => 1
[created_at] => 2011-06-04 15:11:50
[updated_at] => 2012-03-05 14:04:54
[is_active] => 1
[created_in] => Lenjerii
[prefix] =>
[firstname] => Ovidiu
[middlename] =>
[lastname] => Ungureanu
[suffix] =>
[password_hash] => ****
[taxvat] =>
[facebook_uid] => ****
[gender] => 1
[dob] =>
[autoswitchgroup] => yes
[is_subscribed] => 1
[parent_id] => 0
[dob_is_formated] => 1
[confirmation] =>
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[website_id] => 1
[email] => ****
[group_id] => 11
[increment_id] => 000000001
[store_id] => 1
[created_at] => 2011-06-04 15:11:50
[updated_at] => 2012-03-05 13:49:46
[is_active] => 1
[created_in] => Lenjerii
[prefix] =>
[firstname] => Ovidiu
[middlename] =>
[lastname] => Ungureanu
[suffix] =>
[password_hash] => ****
[taxvat] =>
[facebook_uid] => ****
[gender] => 1
)
[_idFieldName:protected] => entity_id
[_isDeleted:protected] =>
In frontend I want the same info, getting it using this:
$customer = Mage::getSingleton('customer/session')->getCustomer();
Here is the result:
[_resourceCollectionName:protected] => customer/customer_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[website_id] => 1
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[email] => ***
[group_id] => 11
[increment_id] => 000000001
[store_id] => 1
[created_at] => 2011-06-04 15:11:50
[updated_at] => 2012-03-05 13:49:46
[is_active] => 1
[created_in] => Lenjerii
[prefix] =>
[firstname] => Ovidiu
[middlename] =>
[lastname] => Ungureanu
[suffix] =>
[password_hash] => ***
[taxvat] =>
[facebook_uid] => ***
[gender] => 1
[tax_class_id] => 3
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[website_id] => 1
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[email] => ***
[group_id] => 11
[increment_id] => 000000001
[store_id] => 1
[created_at] => 2011-06-04 15:11:50
[updated_at] => 2012-03-05 13:49:46
[is_active] => 1
[created_in] => Lenjerii
[prefix] =>
[firstname] => Ovidiu
[middlename] =>
[lastname] => Ungureanu
[suffix] =>
[password_hash] => ****
[taxvat] =>
[facebook_uid] => ****
[gender] => 1
)
What I'm interested to get it [autoswitchgroup] - the attribute created. This one is not saved in database explicity for all users, that's why I have a default value.
Now what I don't understand is how on module I get it in customer data but on frontend I don't see it.
Even more, on module appears in [_data:protected] but not in [_origData:protected].
In both places the object is _resourceCollectionName:protected] => customer/customer_collection ...
Hopefully this is not too long to read.
I know is not an real answer but here is what I did and works.
$attribute = 'autoswitchgroup';
$customer->getDataSetDefault($attribute, 'yes');
So, I get the attribute name and using getDataSetDefault for that attribute will set a default value if there is no value.

Resources