flatten multidimensional array recursive php - php

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

Related

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);

Left join return duplicate data although I used GROUP BY

SQL:
$sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.* FROM orders
LEFT JOIN order_products ON orders.id=order_products.order_id
LEFT JOIN products ON products.id=order_products.product_id
LEFT JOIN psettings ON psettings.product_id=order_products.product_id
LEFT JOIN cities ON cities.id=orders.city_id
LEFT JOIN locations ON locations.id=orders.location_id
WHERE orders.status = '$status'
ORDER BY orders.id ASC ";
It returns unique data. Here is the returned data:
Array
(
[orders] => Array
(
[id] => 12
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => confirmed
[cashed] => 1115
[created] => 2015-07-02 01:07:18
[modified] => 2015-07-02 01:01:57
[comment] => 07/02/2015 06:00 am
)
[city] => Array
(
[id] => 2
[name] => comilla
)
[location] => Array
(
[id] => 5
[city_id] => 2
[name] => homna
)
[order_products] => Array
(
[0] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
But When I Add another table to join left with orders table it returns duplicate entry. Additionally I join left 'action_bies' with orders table as follows:
$sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.*,action_bies.* FROM orders
LEFT JOIN order_products ON orders.id=order_products.order_id
LEFT JOIN action_bies ON orders.id=action_bies.order_id AND action_bies.action='$action'
LEFT JOIN products ON products.id=order_products.product_id
LEFT JOIN psettings ON psettings.product_id=order_products.product_id
LEFT JOIN cities ON cities.id=orders.city_id
LEFT JOIN locations ON locations.id=orders.location_id
WHERE orders.status = '$status'
ORDER BY orders.id ASC ";
This sql return duplicate data. Here is the data:
Array
(
[orders] => Array
(
[id] => 12
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => confirmed
[cashed] => 1115
[created] => 2015-07-02 01:07:18
[modified] => 2015-07-02 01:01:57
[comment] => 07/02/2015 06:00 am
)
[city] => Array
(
[id] => 2
[name] => comilla
)
[location] => Array
(
[id] => 5
[city_id] => 2
[name] => homna
)
[action] => Array
(
[0] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[1] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[2] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[3] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[4] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[5] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[6] => Array
(
[id] => 1
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 0000-00-00 00:00:00
)
[7] => Array
(
[id] => 4
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
[8] => Array
(
[id] => 5
[action] => confirm
[admin_id] => 30
[order_id] => 12
[created] => 2015-07-02 00:00:00
)
)
[order_products] => Array
(
[0] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[2] => Array
(
[id] => 10
[order_id] => 12
[product_id] => 1
[pieces] => 1
)
[3] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[4] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[5] => Array
(
[id] => 11
[order_id] => 12
[product_id] => 2
[pieces] => 1
)
[6] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
[7] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
[8] => Array
(
[id] => 12
[order_id] => 12
[product_id] => 3
[pieces] => 3
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[2] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[3] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[4] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[5] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[6] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[7] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[8] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[1] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-29 15:15:58
[bppp] => 165
[sppp] => 300
[discount] => 20
[service_charge] => 30
)
[3] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[4] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[5] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[6] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
[7] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
[8] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
Here it should be mentioned that a action_bies table has duplicate data as follows:
How can I get unique data in this case. Thanks to read this large data.
You can select DISTINCT on the most unique identifier you have, which would be orders_products.id most likely.
Example:
SELECT DISTINCT(orders_products.id) FROM orders_products WHERE x = '$y';
I don't see a GROUP BY statement in your original query.

formatting an array using php

I have the following array which have duplicate data:
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[products] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[psettings] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
)
[1] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 6
[order_id] => 10
[product_id] => 1
[pieces] => 1
)
[products] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[psettings] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
)
[2] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[products] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[psettings] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
)
[3] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 5
[order_id] => 10
[product_id] => 2
[pieces] => 1
)
[products] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[psettings] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
)
[4] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
[products] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[psettings] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
[5] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 7
[order_id] => 10
[product_id] => 3
[pieces] => 1
)
[products] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[psettings] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
I want to format this array and produce the following array:
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[0] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
[1] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[0] => Array
( [id] => 6
[order_id] => 10
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 5
[order_id] => 10
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 7
[order_id] => 10
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
)
How can I do this. I reformat simple array but this does not make sense to me to format. It makes me cry. Please help me. If any helper function is suggested it will be really a gift. Thanks in advance.
You need to do like below:-
<?php
$firstArray = Array
(
'0' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 2,
'order_id' => 9,
'product_id' => 1,
'pieces' => 1
),
'products' => Array
(
'id' => 1,
'category_id' => 1,
'name' => 'নভোযানের নাম সি প্রোগ্রামিং',
'writer' => 'Engr. Abdus Sattar Bhuiyan',
'created' => '2015-06-24 16:17:45'
),
'psettings' => Array
(
'id' => 1,
'category_id' => 1,
'product_id' => 1,
'img' => '1.jpg',
'desc' => 'description',
'created' => '2015-06-28 00:28:26',
'bppp' => 44000,
'sppp' => 45000,
'discount' => 25,
'service_charge' => 30
),
),
'1' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 3,
'order_id' => 9,
'product_id' => 2,
'pieces' => 1
),
'products' => Array
(
'id' => 2,
'category_id' => 1,
'name' => 'Resonance of creativity with C++',
'writer' => 'Engr. Abdus Sattar Bhuiyan',
'created' => '2015-06-26 07:32:52'
),
'psettings' => Array
(
'id' => 2,
'category_id' => 1,
'product_id' => 2,
'img' => '2.jpg',
'desc' => '',
'created' => '2015-06-26 07:33:41',
'bppp' => 150,
'sppp' => 250,
'discount' => 20,
'service_charge' => 30
),
),
'2' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 4,
'order_id' => 9,
'product_id' => 3,
'pieces' => 1
),
'products' => Array
(
'id' => 3,
'category_id' => 1,
'name' => 'programming by story C',
'writer' => 'Hasibul Hasan Shanto',
'created' => '2015-06-26 07:35:57'
),
'psettings' => Array
(
'id' => 3,
'category_id' => 1,
'product_id' => 3,
'img' => '3.jpg',
'desc' => '',
'created' => '2015-06-26 07:36:26',
'bppp' => 150,
'sppp' => 250,
'discount' => 10,
'service_charge' => 30
),
),
);
$new_array = array();
foreach($firstArray as $key=> $arr){
if($key == 0){
$new_array[0]['orders'] = $arr['orders'];
$new_array[0]['order_products'][] = $arr['order_products'];
$new_array[0]['products'][] = $arr['products'];
$new_array[0]['psettings'][] = $arr['psettings'];
}else{
foreach($new_array as $key=> $newarr){
if($arr['orders'] == $newarr['orders']){
$new_array[$key]['order_products'][] = $arr['order_products'];
$new_array[$key]['products'][] = $arr['products'];
$new_array[$key]['psettings'][] = $arr['psettings'];
}else{
$new_array[] = $arr;
}
}
}
}
echo "<pre/>";print_r($new_array);
?>
Output:- https://eval.in/388565.
Note:- don't worry about the array i putted. I just take your array and formatted it for running purpose and checking at my end. thanks.
This is a fairly simple job for array_column() (manual reference). Like this ($array is your source):
$composite = [];
$composite['orders'] = array_column($array, 'orders')[0];
$composite['order_products'] = array_column($array, 'order_products');
$composite['products'] = array_column($array, 'products');
$composite['psettings'] = array_column($array, 'psettings');
print_r($composite);
This results in the following array:
Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[0] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
If you need to aggregate multiple such arrays into one master array that contains it all, then you can for example wrap it inside a simple loop and add a numeric index for each composite set:
$composite = [];
foreach($arrays as $num=>$array) {
$composite[$num]['orders'] = array_column... // etc.
}
Also make note of the index_key feature of array_column mentioned in the manual, may come in handy if you want to use the order IDs as the main keys --- though for that, you'd have to re-factor the loop logic a bit. Let me know if it's a relevant concern and I'll add more; or if this is enough as-is.

How to re-arrange Array in php

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.

How to print assosiative array data

Array
(
[0] => stdClass Object
(
[id] => 7
[price] => 1300
[discount] => 13
[early_bird_date] => 2014-12-30
[timing] =>
[batch] =>
[slug] =>
[status] => 1
[ktw_course_id] => 5
[ktw_country_id] => 2
[ktw_city_id] => 5
[ktw_timeslot_id] => 0
[ktw_training_mode_id] => 3
[ktw_currency_id] => 6
[created] =>
[modified] =>
[course] => jquery
[country] => kuwait
[city] => dubai
[currency] => EUR
[workshop_dates] => Array
(
[0] => stdClass Object
(
[id] => 7
[start_date] => 2014-11-04
[end_date] => 2015-01-05
[status] => 1
[ktw_workshop_id] => 7
[created] => 2014-11-28 06:09:41
[modified] => 2014-11-28 06:09:41
)
[1] => stdClass Object
(
[id] => 8
[start_date] => 2015-01-06
[end_date] => 2015-01-07
[status] => 1
[ktw_workshop_id] => 7
[created] => 2014-11-28 06:09:55
[modified] => 2014-11-28 06:09:55
)
[2] => stdClass Object
(
[id] => 9
[start_date] => 2015-01-08
[end_date] => 2015-01-09
[status] => 1
[ktw_workshop_id] => 7
[created] => 2014-11-28 06:10:15
[modified] => 2014-11-28 06:10:15
)
)
)
[1] => stdClass Object
(
[id] => 5
[price] => 900
[discount] => 13
[early_bird_date] => 2014-11-26
[timing] =>
[batch] =>
[slug] =>
[status] => 1
[ktw_course_id] => 5
[ktw_country_id] => 1
[ktw_city_id] => 2
[ktw_timeslot_id] => 0
[ktw_training_mode_id] => 1
[ktw_currency_id] => 1
[created] =>
[modified] => 2014-11-14 14:34:29
[course] => jquery
[country] => india
[city] => vizag
[currency] => INR
[workshop_dates] => Array
(
[0] => stdClass Object
(
[id] => 2
[start_date] => 2014-11-28
[end_date] => 2014-11-29
[status] => 1
[ktw_workshop_id] => 5
[created] =>
[modified] =>
)
)
)
[2] => stdClass Object
(
[id] => 6
[price] => 1300
[discount] => 13
[early_bird_date] => 2014-12-30
[timing] =>
[batch] =>
[slug] =>
[status] => 1
[ktw_course_id] => 5
[ktw_country_id] => 1
[ktw_city_id] => 4
[ktw_timeslot_id] => 0
[ktw_training_mode_id] => 2
[ktw_currency_id] => 1
[created] =>
[modified] => 2014-11-25 10:16:58
[course] => jquery
[country] => india
[city] => hyderabad
[currency] => INR
[workshop_dates] => Array
(
[0] => stdClass Object
(
[id] => 4
[start_date] => 2014-12-30
[end_date] => 2014-12-31
[status] => 1
[ktw_workshop_id] => 6
[created] => 2014-11-28 06:08:44
[modified] => 2014-11-28 06:08:44
)
[1] => stdClass Object
(
[id] => 5
[start_date] => 2014-12-31
[end_date] => 2015-01-01
[status] => 1
[ktw_workshop_id] => 6
[created] => 2014-11-28 06:08:59
[modified] => 2014-11-28 06:08:59
)
[2] => stdClass Object
(
[id] => 6
[start_date] => 2015-01-02
[end_date] => 2015-01-03
[status] => 1
[ktw_workshop_id] => 6
[created] => 2014-11-28 06:09:14
[modified] => 2014-11-28 06:09:14
)
)
)
)
i want to print start_date and end _date ,please help me how can i get it.
thanks in advance.
This is very beginner level question.
foreach ($YourVariable as $arr_of_objs ) {
foreach ($arr_of_objs->workshop_dates as $obj) {
echo $obj->start_date;
echo $obj->end_date;
# code...
}
}
Try this -
$obj = //your object
$newArray = (array)$obj;//converting object to array
foreach ($newArray as $val) {
foreach ($val['workshop_dates'] as $value) {
echo $value['start_date']. ' ' .$value['end_date'];
}
}
Assuming the objects are implementing ArrayAccess. You would do:
foreach ($object as $obj) {
foreach($obj[workshop_dates] as $workshopdate) {
$startDate = $workshopdate ['start_date'];
$endDate = $workshopdate ['end_date'];
}
}

Categories