Group a multidimensional array by a particular value - arrays

I have a multidimensional array and am trying to group them according to the value of array properties.
I'm trying to group them by id, but I won't actually know the id beforehand. So, it's not like I can put it in a for loop and say while i < 7, because I won't know that 7 is the maximum value for the id value,
Array (
[0] => Array (
[name] => R8900
[type] => public
[id] => 1
)
[1] => Array (
[name] => R8944
[type] => public
[id] => 1
)
[2] => Array (
[name] => R8922
[type] => private
[id] => 3
)
[3] => Array (
[name] => R8816
[type] => private
[id] => 3
)
[4] => Array (
[name] => R7434
[type] => VIP
[id] => 7
)
)
What I'm hoping to produce:
Array (
[1] => Array (
[0] => Array (
[name] => R8900
[type] => public
)
[1] => Array (
[name] => R8944
[type] => public
)
)
[3] => Array (
[2] => Array (
[name] => R8922
[type] => private
)
[3] => Array (
[name] => R8816
[type] => private
)
)
[7] => Array (
[4] => Array (
[name] => R7434
[type] => VIP
)
)
)

Something as simple as:
var result:Object = {};
for each(var i:Object in input)
{
if(!result.hasOwnProperty(i.id))
{
result[i.id] = [];
}
result[i.id].push(i);
delete i.id;
}

Related

Reindex PHP Nested Array

After printing my $my_values['outer_group']['fieldset'] I'm getting the following output as per the data:
Array
(
[fieldset] => Array
(
[1] => Array
(
[title] => Dummy_Value1
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[2] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[3] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[actions] => Array
(
[add] => Add InnerGroup
[remove] => Remove InnerGroup
)
)
)
)
[2] => Array
(
[title] => Dummy_Value2
[inner_group] => Array
(
[fieldset] => Array
(
[1] => Array
(
[id] => 21
[title] => Dummy_Value21
)
[actions] => Array
(
[add] => Add InnerGroup
)
)
)
)
[actions] => Array
(
[add] => Add OuterGroup
[remove] => Remove OuterGroup
)
)
)
My requirement is to re-index the output data, hence I've performed the following code to re-index the same:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
$inner = [];
foreach ($outer_types as $outer_key => $outer_value) {
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}
As per the output its getting re-indexed but with some errors in data. I'm getting trouble while populating the [inner] data, following is the output for the same:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
[3] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
Whereas, it should be:
Array
(
[0] => Array
(
[title] => Dummy_Value1
[inner] => Array
(
[0] => Array
(
[id] => 11
[title] => Dummy_Value11
)
[1] => Array
(
[id] => 12
[title] => Dummy_Value12
)
[2] => Array
(
[id] => 13
[title] => Dummy_Value13
)
)
)
[1] => Array
(
[title] => Dummy_Value2
[inner] => Array
(
[0] => Array
(
[id] => 21
[title] => Dummy_Value21
)
)
)
)
$inner = []; needs to be within the foreach loop so that it is empty before building each internal element.
Untested - but as follows:
<?php
if (isset($my_values['outer_group']) && !empty($my_values['outer_group'])) {
$outer_types = $my_values['outer_group']['fieldset'];
foreach ($outer_types as $outer_key => $outer_value) {
$inner = [];
if (is_numeric($outer_key)) {
if (isset($outer_value['inner_group']['fieldset'])) {
foreach ($outer_value['inner_group']['fieldset'] as $k => $v) {
if (is_numeric($k)) {
$inner[] = [
'id' => $v['id'],
'title' => !empty($v['title']) ? $token->replace($v['title']) : NULL,
];
}
}
}
$my_values['outer'][$outer_key] = [
'title' => !empty($outer_value['title']) ? $token->replace($outer_value['title']) : NULL,
'inner' => $inner,
];
}
}
}

SalesForce Analytics API filter problems

I am trying to use the Salesforce Analytics api to get a report filtered by date, but when I apply the date filters it does not seem to stick, only the default filter applies.
Here is the POST data that I am sending
{
"reportMetadata": {
"aggregates": [
"FORMULA1",
"FORMULA2",
"FORMULA3",
"RowCount"
],
"currency": null,
"detailColumns": [
"SUBJECT",
"DUE_DATE",
"PRIORITY",
"STATUS",
"TASK",
"ACCOUNT",
"CONTACT",
"LEAD",
"OPPORTUNITY"
],
"developerName": "me",
"groupingsAcross": [
{
"dateGranularity": "None",
"name": "CALLDISPOSITION",
"sortAggregate": null,
"sortOrder": "Asc"
}
],
"groupingsDown": [
{
"dateGranularity": "None",
"name": "ASSIGNED",
"sortAggregate": null,
"sortOrder": "Asc"
}
],
"historicalSnapshotDates": [],
"id": "00OC0000006nlWpMAI",
"name": "report",
"reportBooleanFilter": "1 AND 2 AND 3",
"reportFilters": [
{
"column": "CALLTYPE",
"operator": "equals",
"value": "Outbound"
},
{
"value": "2014-08-01",
"column": "Activity.qbdialer__Call_Date_Time__c",
"operator": "greaterOrEqual"
},
{
"value": "2014-08-31",
"column": "Activity.qbdialer__Call_Date_Time__c",
"operator": "lessOrEqual"
}
],
"reportFormat": "MATRIX",
"reportType": {
"label": "Tasks and Events",
"type": "Activity"
}
}
}
and here is what I get back
Array
(
[status] => Array
(
[http_code] => 200
)
[contents] => Array
(
[attributes] => Array
(
[completionDate] => 2014-08-19T17:23:26Z
[id] => 0LGC00000024PlIOAU
[ownerId] => 005C0000003KHKbIAO
[reportId] => 00OC0000006nlWpMAI
[reportName] => report
[requestDate] => 2014-08-19T17:23:25Z
[status] => Success
[type] => ReportInstance
)
[allData] => 1
[factMap] => Array
(
[T!2] => Array
(
[aggregates] => Array
(
[0] =>
[1] => Array
(
[label] => 6.67%
[value] => 6.66666667
)
[2] =>
[3] => Array
(
[label] => 1
[value] => 1
)
)
)
[T!T] => Array
(
[aggregates] => Array
(
[0] =>
[1] =>
[2] =>
[3] => Array
(
[label] => 15
[value] => 15
)
)
)
[T!1] => Array
(
[aggregates] => Array
(
[0] =>
[1] => Array
(
[label] => 13.33%
[value] => 13.33333333
)
[2] =>
[3] => Array
(
[label] => 2
[value] => 2
)
)
)
[0!0] => Array
(
[aggregates] => Array
(
[0] => Array
(
[label] => 3.00
[value] => 3
)
[1] =>
[2] => Array
(
[label] => 0.25
[value] => 0.25
)
[3] => Array
(
[label] => 12
[value] => 12
)
)
)
[0!T] => Array
(
[aggregates] => Array
(
[0] =>
[1] =>
[2] =>
[3] => Array
(
[label] => 15
[value] => 15
)
)
)
[T!0] => Array
(
[aggregates] => Array
(
[0] =>
[1] => Array
(
[label] => 80.00%
[value] => 80
)
[2] =>
[3] => Array
(
[label] => 12
[value] => 12
)
)
)
[0!2] => Array
(
[aggregates] => Array
(
[0] => Array
(
[label] => 0.00
[value] => 0
)
[1] =>
[2] => Array
(
[label] => 0.00
[value] => 0
)
[3] => Array
(
[label] => 1
[value] => 1
)
)
)
[0!1] => Array
(
[aggregates] => Array
(
[0] => Array
(
[label] => 1.00
[value] => 1
)
[1] =>
[2] => Array
(
[label] => 0.50
[value] => 0.5
)
[3] => Array
(
[label] => 2
[value] => 2
)
)
)
)
[groupingsAcross] => Array
(
[groupings] => Array
(
[0] => Array
(
[groupings] => Array
(
)
[key] => 0
[label] => -
[value] =>
)
[1] => Array
(
[groupings] => Array
(
)
[key] => 1
[label] => Contact
[value] => Contact
)
[2] => Array
(
[groupings] => Array
(
)
[key] => 2
[label] => Correct Contact
[value] => Correct Contact
)
)
)
[groupingsDown] => Array
(
[groupings] => Array
(
[0] => Array
(
[groupings] => Array
(
)
[key] => 0
[label] => first sftest1
[value] => 005C0000003KHKbIAO
)
)
)
[hasDetailRows] =>
[reportExtendedMetadata] => Array
(
[aggregateColumnInfo] => Array
(
[RowCount] => Array
(
[acrossGroupingContext] =>
[dataType] => int
[downGroupingContext] =>
[label] => Record Count
)
[FORMULA1] => Array
(
[acrossGroupingContext] => CALLDISPOSITION
[dataType] => double
[downGroupingContext] => ASSIGNED
[label] => Ring Time
)
[FORMULA3] => Array
(
[acrossGroupingContext] => CALLDISPOSITION
[dataType] => double
[downGroupingContext] => ASSIGNED
[label] => Average Ring Time
)
[FORMULA2] => Array
(
[acrossGroupingContext] => CALLDISPOSITION
[dataType] => percent
[downGroupingContext] => GRAND_SUMMARY
[label] => Group Average
)
)
[detailColumnInfo] => Array
(
[SUBJECT] => Array
(
[dataType] => string
[label] => Subject
)
[DUE_DATE] => Array
(
[dataType] => date
[label] => Date
)
[PRIORITY] => Array
(
[dataType] => picklist
[label] => Priority
)
[STATUS] => Array
(
[dataType] => picklist
[label] => Status
)
[TASK] => Array
(
[dataType] => boolean
[label] => Task
)
[ACCOUNT] => Array
(
[dataType] => string
[label] => Company / Account
)
[CONTACT] => Array
(
[dataType] => string
[label] => Contact
)
[LEAD] => Array
(
[dataType] => string
[label] => Lead
)
[OPPORTUNITY] => Array
(
[dataType] => string
[label] => Opportunity
)
)
[groupingColumnInfo] => Array
(
[ASSIGNED] => Array
(
[dataType] => string
[groupingLevel] => 0
[label] => Assigned
)
[CALLDISPOSITION] => Array
(
[dataType] => string
[groupingLevel] => 0
[label] => Call Result
)
)
)
[reportMetadata] => Array
(
[aggregates] => Array
(
[0] => FORMULA1
[1] => FORMULA2
[2] => FORMULA3
[3] => RowCount
)
[currency] =>
[detailColumns] => Array
(
[0] => SUBJECT
[1] => DUE_DATE
[2] => PRIORITY
[3] => STATUS
[4] => TASK
[5] => ACCOUNT
[6] => CONTACT
[7] => LEAD
[8] => OPPORTUNITY
)
[developerName] => Ring_Time_By_Agent
[groupingsAcross] => Array
(
[0] => Array
(
[dateGranularity] => None
[name] => CALLDISPOSITION
[sortAggregate] =>
[sortOrder] => Asc
)
)
[groupingsDown] => Array
(
[0] => Array
(
[dateGranularity] => None
[name] => ASSIGNED
[sortAggregate] =>
[sortOrder] => Asc
)
)
[historicalSnapshotDates] => Array
(
)
[id] => 00OC0000006nlWpMAI
[name] => report
[reportBooleanFilter] =>
[reportFilters] => Array
(
[0] => Array
(
[column] => CALLTYPE
[operator] => equals
[value] => Outbound
)
)
[reportFormat] => MATRIX
[reportType] => Array
(
[label] => Tasks and Events
[type] => Activity
)
)
)
)
As you can see in the results only the CALLTYPE column filter applied (this is the default filter).
Does anyone see anything wrong with the POST data that I am sending?
I have tried using other reports and other date columns to filter on, but it is always the same, only the default filters apply.
I found the answer. I was not setting the header Content-type to application/json. Once I did that the filters started working.

Cakephp Model Assosiation Joins

well, in my cakephp project, i have 6 models, they are {User,Property,Category,Status,Comments,Attachable}
Property Model : $belongsTo = {User,Category,Status} . AND $hasMany = {Comment,Attachable} ..
Properties Controller Index Method ..
public function index() {
$this->Property->recursive = 0;
$this->set('properties', $this->paginate());
$properties= $this->paginate();
//pr($properties);
//exit;
}
which outputs :
Array
(
[0] => Array
(
[Property] => Array
(
[id] => 1
[user_id] => 1
[category_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
[userLevel] => 1
)
[Category] => Array
(
[id] => 1
[name] => villa
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[property_count] => 0
)
)
[1] => Array
(
[Property] => Array
(
[id] => 2
[user_id] => 1
[phone] => 78666
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villas are iby a wealthy person.
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[description] => New Property
[property_count] => 0
)
)
[2] => Array
(
[Property] => Array
(
[id] => 3
[user_id] => 1
[category_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
[email] => admin#realty.com
[userLevel] => 1
)
[Category] => Array
(
[id] => 1
[name] => villa
[property_count] => 0
)
[Status] => Array
(
[id] => 1
[name] => New
[property_count] => 0
)
)
)
Well the above method retrieves data only from the models in $belongsTo variable. What I want is to combine the related Comments and Attachable Models with the same
query. This can be manually queried with LEFT JOIN to comments and attachable MODEL.
this is what the query looks like
function index() {
$properties = $this->Property->query("SELECT `Property`.`id`, `Property`.`user_id`, `Property`.`category_id`, `Property`.`status_id`, `Property`.`state_id`, `User`.`id`, `User`.`name`, `User`.`username`,`Category`.`id`, `Category`.`name`, `Category`.`description`, `Status`.`name`, `Status`.`description``Comment`.`id`,`Comment`.`name`,`Comment`.`comment`,`Attachment`.`id`,`Attachment`.`AttachmentName` FROM `properties` AS `Property`
LEFT JOIN `users` AS `User` ON (`Property`.`user_id` = `User`.`id`)
LEFT JOIN `categories` AS `Category` ON (`Property`.`category_id` = `Category`.`id`)
LEFT JOIN `statuses` AS `Status` ON (`Property`.`status_id` = `Status`.`id`)
LEFT JOIN `cities` AS `City` ON (`Property`.`city_id` = `City`.`id`)
LEFT JOIN `comments` AS `Comment` ON (`Property`.`id` = `Comment`.`id`)
LEFT JOIN `attachments` AS `Attachment` ON (`Property`.`id` = `Attachment`.`id`) WHERE 1 = 1 LIMIT 20");
//pr($properties);
//exit;
$this->set('properties',$properties);
}
which outputs :
Array
(
[0] => Array
(
[Property] => Array
(
[id] => 1
[user_id] => 1
[category_id] => 1
[status_id] => 1
)
[User] => Array
(
[id] => 1
[name] => za
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villa by a wealthy person.
)
[Status] => Array
(
[name] => New
)
[Comment] => Array
(
[id] => 1
[name] => A
[comment] => hello
)
[Attachment] => Array
(
[id] => 1
[AttachmentName] => 1342009083_4c2380.jpg
)
)
[1] => Array
(
[Property] => Array
(
[id] => 2
[user_id] => 1
[category_id] => 1
[status_id] => 1
)
[User] => Array
(
[id] => 1
[name] => zals
[username] => admin
)
[Category] => Array
(
[id] => 1
[name] => villa
[description] => villas a
)
[Status] => Array
(
[name] => New
)
[Comment] => Array
(
[id] => 2
[name] => asdasd
[comment] => asdasdas
)
[Attachment] => Array
(
[id] => 2
[AttachmentName] => 92f2e3c067d731a3823762.jpg
)
)
)
I am sure the latter way is not the right way to do it. Or
Suppose I used the cakephp's default index method in controller, And in VIEW i can do nested foreach loops inside the main foreach loop .. i .e
<?php foreach ($properties as $property) { ?>
$id = $property['Property']['id'];
$comments = ..... //here to query for the associated comments passing the id value ..
?>
can this be sorted out this way ? or how ? please guide .. thanks ..
Two things:
You can probably solve your problem by using the Containable behaviour. It's very flexible and can be used with pagination.
Doing an inner join on one-to-many relation is probably not a good idea.

CakePHP find screwed up

I have a simple database table which looks like this: http://www.xup.in/dl,17431410/screwed.jpg/ Now I want the data using $this->find('all') in the ItemModel which looks like:
public function getList() {
$data = $this->find('all');
pr($data);
exit;
}
The output is
Array
(
[0] => Array
(
[Champion] => Array
(
[id] => 1
[name] => A
[key] => ahri
)
)
[1] => Array
(
[Champion] => Array
(
[id] => 2
[name] => A
[key] => akali
)
)
[2] => Array
(
[Champion] => Array
(
[id] => 3
[name] => A
[key] => alistar
)
)
[3] => Array
(
[Champion] => Array
(
[id] => 4
[name] => A
[key] => amumu
)
)
)
I have absolutely no idea why the name is cut at the first letter.
schema looks good to me.
did you clear the cache? the old scheme that is cached in /tmp/cache/persistent might still have something else stored.
maybe a tinyint, or char 1 etc.

find value in array cakephp

Hey guys I am trying to get a value from an array in my events_controller.php file.
Event belongsTo Entity and Entity hasMany Event. I need this value to perform some other logic but im really stuck and i know it should be an easy thing to do.
I am trying to get the value of Entity.user_id from this array.
Array
(
[Event] => Array
(
[id] => 19
[entity_id] => 8
[name] => new event
[time_start] => 2011-02-26 19:09:00
[time_end] => 2011-02-26 19:09:00
[dateStart] => 0000-00-00
[dateEnd] => 0000-00-00
[description] => jgiuguygo
[ageRange] => 67
)
[Entity] => Array
(
[id] => 8
[user_id] => 14
[location_id] => 15
[type] => EVENT
)
[Eventfeedback] => Array
(
)
)
the above matrix i obtained with this code:
$value = $this->Event->read();
pr($value);
Now this is as close as I can get...
Array
(
[Entity] => Array
(
[user_id] => 14
)
[Event] => Array
(
[id] => 19
)
[Eventfeedback] => Array
(
)
)
with this code
$value = $this->Event->read('Entity.user_id');
pr($value);
An last try i got this array
Array
(
[Entity] => Array
(
[id] => 1
[user_id] => 11
[location_id] => 8
[type] => sdfsdfdsf
)
[User] => Array
(
[id] => 11
[firstName] => luis
[lastName] => pooya
[username] => admin
[password] => 94c882c8506497a9f031ca5a4db6d0143c97fe45
[role] => admin
[email] => some
)
[Location] => Array
(
[id] => 8
[name] => First Nation University of Canada
[xCoordinate] => 0
[yCoordinate] => 0
)
[Establishment] => Array
(
)
[Event] => Array
(
)
[Vmachine] => Array
(
)
)
with this code
$value = $this->Event->Entity->find('user_id');
pr($value);
Hope someone can help me out. Thanks in advance.Luis
I'm not sure I understood you correctly. But to get user_id in your examples would be like
$value = $this->Event->read('Entity.user_id');
pr($value['Entity']['user_id']);
$event = $this->Event->read();
$userId = $event['Entity']['user_id'];

Resources