I have table which have these fields patchno, Project_no Item_Desc Quantity Store Action
And currently it looks like this:
I expect to be as this:
Every patch_no must have unique project_no and store other two columns are variety.
My array variable which is PHP if I use print_r($orders) it outputs this.
Array ( [0] => stdClass Object ( [ID] => 1 [patchno] => 1 [item_id] => 1 [Quantity] => 10 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-22 14:55:51 [Desc] => Jiingado [Cost] => 6.60 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ) [1] =>
stdClass Object ( [ID] => 1 [patchno] => 1 [item_id] => 3 [Quantity] => 2 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-22 14:55:51 [Desc] => Marmar [Cost] => 1.00 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ) [2] => stdClass Object ( [ID] => 1 [patchno] => 1000002 [item_id] => 2 [Quantity] => 21 [store_id] => 1 [project_id] => 1 [user_id] => 1 [order_date] => 2015-10-24 15:34:52 [Desc] => Masaabiir [Cost] => 2.00 [store_name] => Nuux [Telephone] => 565656 [email] => email#nuux.com [address] => Gacanlibaax [Project_No] => 466 [Site] => A [owner] => Gaboose [foreman] => Axmed Diiriye ))
the query that outputs these value is this:
SELECT orders.*, items.*, stores.*, projects.*
FROM orders
join items on items.ID=orders.item_id
join stores on stores.ID=orders.store_id
join projects on projects.ID=orders.project_id
Use group_concat to get a comma separated string of all the "variety" values in the group and the unique values of the group should be specified in your group by:
select patchno, Project_no, group_concat(Item), group_concat(Quantity), store
from orders
join items on items.ID=orders.item_id
join stores on stores.ID=orders.store_id
join projects on projects.ID=orders.project_id
group by patchno, project_no, store
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.
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.
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 want to be able to group SpecificationCategory.name as parent category and Specifications under as children so that SpecificationCategory.name does not get repeated in the array.
Is there a way to do this in Cake?
Array
(
[0] => Array
(
[Specification] => Array
(
[id] => 15
[name] => AMD Phenom
[specification_category_id] => 1
[subsubcategory_id] => 1
[created] => 2012-10-16 08:18:27
[modified] => 2012-10-16 08:18:27
)
[SpecificationCategory] => Array
(
[id] => 1
[name] => Processor Type
[created] => 2012-10-15 13:50:03
[modified] => 2012-10-15 13:50:03
)
[Subsubcategory] => Array
(
[id] => 1
[name] => Laptops
[subcategory_id] => 1
[created] => 2012-06-24 02:23:40
[modified] => 2012-06-24 02:23:40
)
)
[1] => Array
(
[Specification] => Array
(
[id] => 12
[name] => AMD E350
[specification_category_id] => 1
[subsubcategory_id] => 1
[created] => 2012-10-16 08:18:02
[modified] => 2012-10-16 08:18:02
)
[SpecificationCategory] => Array
(
[id] => 1
[name] => Processor Type
[created] => 2012-10-15 13:50:03
[modified] => 2012-10-15 13:50:03
)
[Subsubcategory] => Array
(
[id] => 1
[name] => Laptops
[subcategory_id] => 1
[created] => 2012-06-24 02:23:40
[modified] => 2012-06-24 02:23:40
)
)
)
If your just trying to get a listing by SpecificationCategory I would just do the find from that model and the array would list all Specifications under each category. This assumes Specifications hasMany SpecificationCategory.