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);
Related
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
)
How to flatten multidimensional array recursive php.
I tried with this link code but not working. I tried many other links also but not working. This code has been used for creating this array.
public function downline_income($userId = null, $offset = 0) {
$userId = user::id();
$limit = AZ::setting('record_per_page');
$objUser = new stdClass();
$objUser->id = $userId;
$downline = $this->user->getDownline($objUser);
AZ::layout('left-content', array(
'block' => 'account/downline_income',
'user' => $userId,
'total_users' => $total_users,
'pagination' => $pagination,
'q' => $userId,
'data' => $downline,
'offset' => $offset,
));
}
public function getDownline($obj, $level = 0) {
$obj->level = $level;
$where = array('parent_id' => $obj->id);
$this->db->select('users.*');
$this->db->where($where);
$query = $this->db->get('users')->result();
foreach ($query as $objUser) {
$obj->data[] = $this->getDownline($objUser, ($level + 1));
}
return $obj;
}
Here is the array which I need to flatten.
Array
(
[0] => stdClass Object
(
[id] => 1135
[gid] => 4
[parent_id] => 1112
[username] => sdafasdf
[email] => kapisdafsdal#abc.com
[name] => sdafsda
[status] => 0
[registerd] => 2017-04-19 15:44:38
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 5
[user_id] => 1135
[purchase_date] => 2017-04-19 15:44:39
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 500
[amount] => 500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
[1] => stdClass Object
(
[id] => 1134
[gid] => 4
[parent_id] => 1112
[username] => sdaf254
[email] => asadfsad#abc.com
[name] => categoryA
[status] => 0
[registerd] => 2017-04-19 15:36:11
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 4
[user_id] => 1134
[purchase_date] => 2017-04-20 00:00:00
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 250
[amount] => 250
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
[2] => stdClass Object
(
[id] => 1136
[gid] => 4
[parent_id] => 1112
[username] => test
[email] => shrasdaf#abc.com
[name] => test
[status] => 0
[registerd] => 2017-04-20 08:49:25
[last_login] => 2017-04-21 10:42:25
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 7
[user_id] => 1136
[purchase_date] => 2017-04-20 08:49:25
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 2500
[amount] => 2500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
[data] => Array
(
[0] => stdClass Object
(
[id] => 1148
[gid] => 4
[parent_id] => 1136
[username] => test_downline
[email] => kapilsdfasf#abc.com
[name] => test_downline
[status] => 0
[registerd] => 2017-04-21 10:42:56
[last_login] => 2017-04-21 11:08:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 9
[user_id] => 1148
[purchase_date] => 2017-04-21 10:42:56
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 10000
[amount] => 10000
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 2
[data] => Array
(
[0] => stdClass Object
(
[id] => 1150
[gid] => 4
[parent_id] => 1148
[username] => test1_downline1
[email] => kapil25#abc.com
[name] => test1_downline1
[status] => 0
[registerd] => 2017-04-21 11:08:27
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 5
[user_id] => 1150
[purchase_date] => 2017-04-21 11:08:27
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 500
[amount] => 500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 3
)
)
)
[1] => stdClass Object
(
[id] => 1149
[gid] => 4
[parent_id] => 1136
[username] => test_downline2
[email] => kapil123#abc.com
[name] => test_downline2
[status] => 0
[registerd] => 2017-04-21 11:06:35
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 6
[user_id] => 1149
[purchase_date] => 2017-04-21 11:06:35
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 1000
[amount] => 1000
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 2
)
)
)
[3] => stdClass Object
(
[id] => 1137
[gid] => 4
[parent_id] => 1112
[username] => test2
[email] => ishrasdaf1#abc.com
[name] => test 2
[status] => 0
[registerd] => 2017-04-20 08:54:59
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 6
[user_id] => 1137
[purchase_date] => 2017-04-20 08:55:00
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 1000
[amount] => 1000
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
I tried with the below code but it working for only level 2
public function array_flatten($array, $i = 0) {
$flat = array();
foreach ($array as $value) {
if (isset($value->data) && is_array($value->data)) {
$flat = array_merge($flat, $this->array_flatten($value->data));
} else {
$flat[] = $value;
}
}
return $flat;
}
Desire output
Array
(
[0] => stdClass Object
(
[id] => 1135
[gid] => 4
[parent_id] => 1112
[username] => sdafasdf
[email] => kapisdafsdal#abc.com
[name] => sdafsda
[status] => 0
[registerd] => 2017-04-19 15:44:38
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 5
[user_id] => 1135
[purchase_date] => 2017-04-19 15:44:39
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 500
[amount] => 500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
[1] => stdClass Object
(
[id] => 1134
[gid] => 4
[parent_id] => 1112
[username] => sdaf254
[email] => asadfsad#abc.com
[name] => categoryA
[status] => 0
[registerd] => 2017-04-19 15:36:11
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 4
[user_id] => 1134
[purchase_date] => 2017-04-20 00:00:00
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 250
[amount] => 250
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
[2] => stdClass Object
(
[id] => 1136
[gid] => 4
[parent_id] => 1112
[username] => test
[email] => shrasdaf#abc.com
[name] => test
[status] => 0
[registerd] => 2017-04-20 08:49:25
[last_login] => 2017-04-21 10:42:25
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 7
[user_id] => 1136
[purchase_date] => 2017-04-20 08:49:25
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 2500
[amount] => 2500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 1
)
[3] => stdClass Object
(
[id] => 1148
[gid] => 4
[parent_id] => 1136
[username] => test_downline
[email] => kapilsdfasf#abc.com
[name] => test_downline
[status] => 0
[registerd] => 2017-04-21 10:42:56
[last_login] => 2017-04-21 11:08:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 9
[user_id] => 1148
[purchase_date] => 2017-04-21 10:42:56
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 10000
[amount] => 10000
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 2
)
[4] => stdClass Object
(
[id] => 1150
[gid] => 4
[parent_id] => 1148
[username] => test1_downline1
[email] => kapil25#abc.com
[name] => test1_downline1
[status] => 0
[registerd] => 2017-04-21 11:08:27
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 5
[user_id] => 1150
[purchase_date] => 2017-04-21 11:08:27
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 500
[amount] => 500
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 3
)
[5] => stdClass Object
(
[id] => 1149
[gid] => 4
[parent_id] => 1136
[username] => test_downline2
[email] => kapil123#abc.com
[name] => test_downline2
[status] => 0
[registerd] => 2017-04-21 11:06:35
[last_login] => 0000-00-00 00:00:00
[password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
[tranjection_password] =>
[package_id] => 6
[user_id] => 1149
[purchase_date] => 2017-04-21 11:06:35
[confirm_date] => 0000-00-00 00:00:00
[package_name] => USD 1000
[amount] => 1000
[daily_income] => 12
[total_income] => 600
[time_duration] => 60
[level] => 2
)
Let me know if you need anything else.
I don't know why you need to do this complicated flatting , and I'm pretty sure that you can -as it's seems that you are fetching data from database- that you can accomplish this easier that the following way, However you may need to use recursion here, for example :
// Sample data
$arr = [
['level' => 1, 'data' => [['level' => 2],['level' => 3, 'data' => [['level' => 6]]]]],
['level' => 4],
['level' => 9, 'data' => [['level' => 8]]],
];
function flat($data, &$list = [])
{
foreach ($data as $key => $value) {
if (isset($value['data']) && is_array($value['data'])) {
$tmp = $value['data'];
unset($value['data']);
flat($tmp, $list);
}
$list[] = $value;
}
return $list;
}
print_r(flat($arr));
Live sample : https://3v4l.org/Rtn5f
P.S often recursion is more expensive with big arrays
So inside my main Joomla template/index.php file I am doing the following to try and get the category id number of the page the user is currently viewing.
The result should be 9 since that is the id number of the category
jimport('joomla.application.categories');
$category = JCategories::getInstance('hwdMediaShare')->get($this->item->categories[0]->id[0]);
print_r($category);
echo "\n\nCategory ID number is : ".($category)."\n\n";
My var_dump shows this (category id number 9 does exist and is present in the var dump so I should be able to obtain it)
[id] => 9
9:random
Shows category id 9 is there and its name too "random"
JCategoryNode Object ( [id] => root [asset_id] => 0 [parent_id] => 0 [lft] => 0 [rgt] => 21 [level] => 0 [extension] => system [title] => ROOT [alias] => root [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {} [metadesc] => [metakey] => [metadata] => {} [created_user_id] => 42 [created_time] => 2011-01-01 00:00:01 [modified_user_id] => 0 [modified_time] => 0000-00-00 00:00:00 [hits] => 0 [language] => * [numitems] => [childrennumitems] => [slug] => 1:root [assets] => [_parent:protected] => [_children:protected] => Array ( [0] => JCategoryNode Object ( [id] => 9 [asset_id] => 610 [parent_id] => root [lft] => 3 [rgt] => 4 [level] => 1 [extension] => com_hwdmediashare [title] => Random [alias] => random [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {"category_layout":"","image":""} [metadesc] => [metakey] => [metadata] => {"author":"","robots":""} [created_user_id] => 571 [created_time] => 2016-02-10 11:52:24 [modified_user_id] => 571 [modified_time] => 2016-03-16 15:45:36 [hits] => 1107 [language] => * [numitems] => [childrennumitems] => [slug] => 9:random [assets] => [_parent:protected] => JCategoryNode Object *RECURSION* [_children:protected] => Array ( ) [_path:protected] => Array ( [0] => 9:random ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => 1 [_constructor:protected] => hwdMediaShareCategories Object ( [_nodes:protected] => Array ( [root] => JCategoryNode Object *RECURSION* [9] => JCategoryNode Object *RECURSION* ) [_checkedCategories:protected] => Array ( [9] => 1 ) [_extension:protected] => com_hwdmediashare [_table:protected] => #__hwdms_category_map [_field:protected] => category_id [_key:protected] => id [_statefield:protected] => state [_options:protected] => Array ( [table] => #__hwdms_category_map [extension] => com_hwdmediashare [field] => category_id [published] => 0 [access] => true ) ) [_errors:protected] => Array ( ) [note] => [path] => uncategorised/test/uncategorised/random [version] => 1 ) ) [_path:protected] => Array ( ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => [_constructor:protected] => hwdMediaShareCategories Object ( [_nodes:protected] => Array ( [root] => JCategoryNode Object *RECURSION* [9] => JCategoryNode Object ( [id] => 9 [asset_id] => 610 [parent_id] => root [lft] => 3 [rgt] => 4 [level] => 1 [extension] => com_hwdmediashare [title] => Random [alias] => random [description] => [published] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [access] => 1 [params] => {"category_layout":"","image":""} [metadesc] => [metakey] => [metadata] => {"author":"","robots":""} [created_user_id] => 571 [created_time] => 2016-02-10 11:52:24 [modified_user_id] => 571 [modified_time] => 2016-03-16 15:45:36 [hits] => 1107 [language] => * [numitems] => [childrennumitems] => [slug] => 9:random [assets] => [_parent:protected] => JCategoryNode Object *RECURSION* [_children:protected] => Array ( ) [_path:protected] => Array ( [0] => 9:random ) [_leftSibling:protected] => [_rightSibling:protected] => [_allChildrenloaded:protected] => 1 [_constructor:protected] => hwdMediaShareCategories Object *RECURSION* [_errors:protected] => Array ( ) [note] => [path] => uncategorised/test/uncategorised/random [version] => 1 ) ) [_checkedCategories:protected] => Array ( [9] => 1 ) [_extension:protected] => com_hwdmediashare [_table:protected] => #__hwdms_category_map [_field:protected] => category_id [_key:protected] => id [_statefield:protected] => state [_options:protected] => Array ( [table] => #__hwdms_category_map [extension] => com_hwdmediashare [field] => category_id [published] => 0 [access] => true ) ) [_errors:protected] => Array ( ) [note] => [path] => [version] => 1 ) Category ID number is : JCategoryNode
And when I try to echo my output to see my number I see this
Category ID number is : JCategoryNode
Can anyone see what I am doing wrong here and correct me.
It should say
Category ID number is : 9
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 5
[user_id] => 5
[category_id] => 1
[name] => Suger
[weight] => 1
[measurement] => kg
[image] =>
[status] => 1
[created] => 2015-05-29 17:02:25
[updated] => 0000-00-00 00:00:00
)
[Category] => Array
(
[id] => 1
[user_id] => 1
[category_name] => Daily uses
[category_image] =>
[status] => 1
[created] => 2015-05-25 12:56:10
[updated] => 2015-06-25 10:27:40
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 23
[product_id] => 5
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055384
[updated] => 1435055384
)
[1] => Array
(
[id] => 28
[product_id] => 5
[store_id] => 1
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435062129
[updated] => 1435062129
)
)
)
[1] => Array
(
[Product] => Array
(
[id] => 6
[user_id] => 1
[category_id] => 1
[name] => Tea
[weight] => 1
[measurement] => litre
[image] =>
[status] => 1
[created] => 2015-05-29 17:02:48
[updated] => 2015-06-18 18:53:30
)
[Category] => Array
(
[id] => 1
[user_id] => 1
[category_name] => Daily uses
[category_image] =>
[status] => 1
[created] => 2015-05-25 12:56:10
[updated] => 2015-06-25 10:27:40
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 22
[product_id] => 6
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055076
[updated] => 1435055076
)
[1] => Array
(
[id] => 25
[product_id] => 6
[store_id] => 1
[details] =>
[mrp] => 12
[selling_price] => 12
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435059905
[updated] => 1435059905
)
)
)
[5] => Array
(
[Product] => Array
(
[id] => 11
[user_id] => 2
[category_id] => 13
[name] => Real Mix Fruit Juice
[weight] => 200
[measurement] => litre
[image] =>
[status] => 1
[created] => 2015-06-17 12:03:19
[updated] => 2015-06-18 13:05:16
)
[Category] => Array
(
[id] => 13
[user_id] => 2
[category_name] => Soft Drink juices
[category_image] =>
[status] => 1
[created] => 2015-06-17 11:15:33
[updated] => 2015-06-18 18:46:03
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 2
[product_id] => 11
[store_id] => 2
[details] => Real Mix Fruit Juice(200 litre)
[mrp] => 20
[selling_price] => 18
[discount] => 10
[price_difference] => 2
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1434613767
[updated] => 1434613767
)
[1] => Array
(
[id] => 29
[product_id] => 11
[store_id] => 1
[details] => Real Mix Fruit Juice
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435062192
[updated] => 1435123348
)
)
)
[8] => Array
(
[Product] => Array
(
[id] => 14
[user_id] => 2
[category_id] => 13
[name] => Real Apple Juice
[weight] => 1
[measurement] => ml
[image] =>
[status] => 1
[created] => 2015-06-18 13:06:44
[updated] => 0000-00-00 00:00:00
)
[Category] => Array
(
[id] => 13
[user_id] => 2
[category_name] => Soft Drink juices
[category_image] =>
[status] => 1
[created] => 2015-06-17 11:15:33
[updated] => 2015-06-18 18:46:03
)
[Storeproduct] => Array
(
[0] => Array
(
[id] => 24
[product_id] => 14
[store_id] => 2
[details] =>
[mrp] => 10
[selling_price] => 10
[discount] => 0
[price_difference] => 0
[is_deal] => 0
[deal_start_date] =>
[deal_end_date] =>
[status] => 1
[created] => 1435055411
[updated] => 1435055411
)
)
)
)
In the above array there are two same category array .I would like merge the same category array records
I need a output like
Category => array(
//category data,
Products=>array(//product data,
Storeproducts =>array(
//Storeproducts data
)
)
)
PHP is pretty well documented and you can find all of the Array sorting methods on this page.
There are some good examples in the comments as well - all you need to do is to sort the array by category and you are done.
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);