How to count multiple dates in php array - php

How I can count same values in the Array. I have read about the array_count_values() but this function can only count Integer or String values but My problem is that I want to count same dates in the Array, How can I count the same dates in the Array.
I have tried this array_count_values() but this gives me this error.
A PHP Error was encountered
Severity: Warning
Message: array_count_values(): Can only count STRING and INTEGER values!
Filename: controllers/Dashboard.php
Line Number: 72
Backtrace:
File: /opt/lampp/htdocs/blink_app/application/controllers/Dashboard.php
Line: 72
Function: array_count_values
File: /opt/lampp/htdocs/blink_app/index.php
Line: 315
Function: require_once
And Here Is my Array.
Array
(
[0] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[users_id] => 5
[ref_id] => 1
[date] => 2017-11-02
[time_from] => 12:15:00
[time_to] => 13:30:00
[appointment_status] => 2017-11-23
[saloon_services_id] => 3
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq#facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[1] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[users_id] => 6
[ref_id] => 2
[date] => 2017-11-02
[time_from] => 04:30:00
[time_to] => 05:00:00
[appointment_status] => 2017-11-25
[saloon_services_id] => 2
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin#gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[2] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[users_id] => 7
[ref_id] => 3
[date] => 2017-11-04
[time_from] => 03:00:00
[time_to] => 03:30:00
[appointment_status] => 2017-11-28
[saloon_services_id] => 2
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq#facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[3] => stdClass Object
(
[id] => 3
[saloon_staff_id] => 3
[users_id] => 7
[ref_id] => 4
[date] => 2017-11-28
[time_from] => 01:00:00
[time_to] => 02:00:00
[appointment_status] => 2017-11-28
[saloon_services_id] => 1
[saloon_profiles_id] => 1
[phone_number] => 9080809
[email] => mubi#yahoo.com
[appointments_enabled] => 0
[role_in_saloon] => Owner
[name] => mubassir
)
)
Any Help Will be Appreciated.

You can take a column out of multi-dimensional array on which you want to do aggregation / count.
<?php
$array = [['name' => 'foo', 'date' => '2017-11-28'], ['name' => 'bar', 'date' => '2017-11-29'], ['name' => 'baz', 'date' => '2017-11-28']];
$dates = array_column($array, 'date'); // here 1st param is the array
This will give you
Array
(
[0] => 2017-11-28
[1] => 2017-11-29
[2] => 2017-11-28
)
Now you can call array_count_values like:
print_r(array_count_values($dates));
Array
(
[2017-11-28] => 2
[2017-11-29] => 1
)

Related

Group multidimensional arrays by key

I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie

Remove duplicate item from multidimensional array

How would I go about removing duplicate items from my multidimensional object array?
In my sample below, I have 2 items which contain the id value of 4 (fullname is 'Jodie (Y6Y5)').
I need to modify my array so that there are no duplicate id values.
I've tried doing loops to fix it, and I've also tried array_unique().
Does anyone have any better ideas on how I can achieve this?
Example input array:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
[3] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Expect Result:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Best practice (and a technique that you will find implemented in Stackoverflow questions every day) is to assign temporary keys, overwrite any pre-existing subarrays based on the temporary keys, then remove the temporary keys at the end.
This avoids having to do iterated lookups. For highest efficiency in your codes, try to minimize iterated function calls.
Method #1: Remove earlier duplicate occurrences / Retain last occurrences (Demo)
foreach($guardian->guardians as $subarray){
$result[$subarray->id]=$subarray; // assign temporary keys
}
$guardian->guardians=array_values($result); // re-declare and remove temporary keys (re-index the subarrays)
var_export($guardian);
*Alternatively, here is a functional-style one-liner providing the same effect: (Demo)
$guardian->guardians=array_values(array_column((array)$guardian->guardians,NULL,'id'));
var_export($guardian);
For those who don't know, array_column() chokes on objects, so objects must be temporarily converted to arrays.
Method #2: Remove later duplicate occurrences / Retain first occurrences (Demo)
foreach($guardian->guardians as $subarray){
if(!isset($result[$subarray->id])){
$result[$subarray->id]=$subarray; // only store if first occurrence of id
}
}
$guardian->guardians=array_values($result); // re-index
var_export($guardian);
I've managed to get a working answer by doing as following:
$tempGuardians = array();
foreach($guardian->guardians as $key => $value){
if (in_array($value->id, $tempGuardians)){
unset($guardian->guardians[$key]);
} else {
array_push($tempGuardians, $value->id);
}
}
unset($tempGuardians);
If anyone knows any cleaner ways or better ways of doing this, I'm open to new ways.
The answer is included on your question, you already said it: array_unique:
$newArray=array_unique($a1);

How to combine specific value in two array to one array with exception?

I have following two different array
First array:
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[1] => stdClass Object
(
[course_id] => 22
[session_id] => 17
[course_name] => Session 1 Course 2
[course_description] => Session 1 Course 2
[course_days] => 3
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 24
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[2] => stdClass Object
(
[course_id] => 23
[session_id] => 17
[course_name] => Session 1 Course 3
[course_description] => Session 1 Course 3
[course_days] => 5
[current_rate] => 2000
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] => 27
[additional_time] =>
[location] =>
[course_active] => 1
)
[3] => stdClass Object
(
[course_id] => 26
[session_id] => 19
[course_name] => Session 3 Course 2
[course_description] => Session 3 Course 2
[course_days] => 2
[current_rate] => 400
[old_rate] =>
[primary_tutor_id] => 29
[secondary_tutor_id] => 29
[additional_time] =>
[location] =>
[course_active] => 1
)
[4] => stdClass Object
(
[course_id] => 27
[session_id] => 19
[course_name] => Session 3 Course 3
[course_description] => Session 3 Course 3
[course_days] => 1
[current_rate] => 200
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] =>
[additional_time] =>
[location] =>
[course_active] => 1
)
)
Second Array:
Array
(
[0] => stdClass Object
(
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
[1] => stdClass Object
(
[discount_id] => 106
[session_id] => 19
[no_course] => 2
[discount] => 20
)
)
I am trying to combine second array all key and value into first array like following example
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
)
I have have tried following code and many other code but i cant get perfect solution. In this two array there is one exception if both [session_id] is same than need array merging other wise as it is array with out merging :
$main=array();
foreach ($courses as $key => $val) {
foreach ($session_discount as $se) {
if ($se->session_id == $val->session_id)
$main[$key] = $val;
array_push($main[$key], $session_discount[0]);
}
}
Need help..! Thanks in Advance.

how to return object data of multiple indexing without foreach in php

I fetch data of particular table by stored procedure ,demo code is
Array
(
[0] => Array
(
[object_types] => Array
(
[ID] => 11
[Code] => Item001
[Name] => Item
[Description] => Items
[DisplayName] => Items
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] =>
[DefaultNarration] =>
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-02 00:00:00
[ModifiedDate] => 2014-04-08 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[1] => Array
(
[object_types] => Array
(
[ID] => 12
[Code] => Uom001
[Name] => Uom
[Description] => Uom
[DisplayName] => Uom
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 1
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-02 00:00:00
[ModifiedDate] => 2014-04-02 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] => 1
[IsDelete] => 1
)
)
[2] => Array
(
[object_types] => Array
(
[ID] => 13
[Code] => Role
[Name] => Role
[Description] => Role
[DisplayName] => Role
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] =>
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-03 00:00:00
[ModifiedDate] => 2014-04-03 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[3] => Array
(
[object_types] => Array
(
[ID] => 14
[Code] => User
[Name] => User
[Description] => Use
[DisplayName] => User
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 71
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-03 00:00:00
[ModifiedDate] => 2014-04-09 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[4] => Array
(
[object_types] => Array
(
[ID] => 15
[Code] => AccountMaster
[Name] => AccountMaster
[Description] => AccountMaster
[DisplayName] => Account
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 1
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-05 00:00:00
[ModifiedDate] => 2014-04-05 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] =>
)
)
[5] => Array
(
[object_types] => Array
(
[ID] => 16
[Code] => Contact
[Name] => Contact
[Description] => Contact
[DisplayName] => Contact
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 103
[DefaultNarration] => 71
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-07 00:00:00
[ModifiedDate] => 2014-04-08 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] => 1
[IsDelete] => 1
)
)
)
I want to retrun all data without foreach looping statement.
$data = array();
foreach($modelData as $row){
$data[] = $row[$model->name];
}
return $data;
I didnt want to use this foreach loop. so please suggest me appropriate solution.
You can use array_map function
do something like :
$data = array_map(function($model){
return $model['object_type']['Name'];
},$modelData);
the code is not tested, but it should be something like this
Based on what your var_dump response was to the other solution, try :
$data = array_map(function($model){
return $model['object_type']['Name']['text'];
},$modelData);
How about JSON? You can get a string from array.
http://www.php.net/manual/en/function.json-encode.php
I'm not sure if this is what you wanted but in case it is, I tried doing the same thing, to get an array without the alias like: $array['first_name'] instead of $array['User']['first_name'] in order to print it in JSON format, but unfortunatelly there is no way to natively do that, so I sticked to the foreach
If not foreach, then try for because to iterate arrays you have to use some loops and the fastest is for as below
$data = array();
$size = sizeof($modelData);
for($i=0; $i<=$size; $i++)
{
$data[] = $modelData[$i][$model->name];
}
return $data;
try
$final_array = array_map(
function ($a) {
return $a['object_types']['Name'];
},
$modelData
);
print_r($final_array);

Merging php arrays if "clientid" is identical?

I have a query which gives back an array, I'd like to be able to merge the array values which have the same "clientid"
Here's my query:
SELECT * FROM #__db_clients_trip WHERE clientid IN (1999, 2984, 1681) AND companyid IN (1,2)
Here's my array:
stdClass Object
(
[id] => 61
[trip_code] => EUR0600
[clientid] => 1999
[date] => 2000-06-17
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 89
[trip_code] =>
[clientid] => 2984
[date] => 2000-03-18
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 176
[trip_code] => EUR0799
[clientid] => 1999
[date] => 1999-07-09
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 281
[trip_code] => EUR0299
[clientid] => 1681
[date] => 1999-03-01
[invoice] => 30666
[aud] => 1000
[wsale] => 950
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
stdClass Object
(
[id] => 296
[trip_code] => EUR0799
[clientid] => 1681
[date] => 1999-07-15
[invoice] =>
[aud] => 0
[wsale] => 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2
)
Is this possible?
EDIT:
To either show a single row per client id, or to merge the array like so:
stdClass Object
(
[id] => 61
[trip_code] => EUR0600, EUR0799
[clientid] => 1999
[date] => 2000-06-17, 1999-07-09
[invoice] =>
[aud] => 0, 0
[wsale] => 0, 0
[margin] =>
[comments] =>
[comments_by] =>
[companyid] => 2, 2
)
^^ or something like that? I'm guessing a single row per clientid would be easier..?
If you just want a single row, add this to the end of the SQL:
GROUP BY clientid
If you want to get (for instance), the latest date for all the records matching a clientid, change:
SELECT *
To:
SELECT *, MAX(date) AS latest_date
You can use functions like MAX, MIN and SUM on fields to get a 'merged' view when using GROUP BY
sounds like you should loop over your result set and do the merge yourself... Use a 2 dimensional array...
If all you care about is a distinct clientID, then you should use the Distinct keyword:
select distinct(clientID) from clients

Categories