How to print assosiative array data - php

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'];
}
}

Related

flatten multidimensional array recursive 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

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.

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.

PHP nested array remove specific keys by condition

I am using PHP 5.5.2 with MySQL back-end. I have a self-referencing database table from that I am getting an array as below :
Array
(
[0] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => Aaa
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 48
[s_id] => 37
[m_id] =>
[title] => bbbbbbbbbbbbb
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 49
[s_id] => 38
[m_id] =>
[title] => cccccccccc
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 50
[s_id] => 39
[m_id] =>
[title] => ddddddddd dddd
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[2] => Array
(
[id] => 51
[s_id] => 40
[m_id] =>
[title] => eeeeeee eeeeeee.
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 54
[s_id] => 18
[m_id] =>
[title] => fffffffff ffff
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 55
[s_id] => 19
[m_id] =>
[title] => gggggg gggggg
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 56
[s_id] => 20
[m_id] =>
[title] => hhhhhhhhhh hhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 57
[s_id] => 21
[m_id] =>
[title] => iiiiiiii iiii
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
[0] => Array
(
[id] => 58
[s_id] => 22
[m_id] =>
[title] => jjjjjjj jjj
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 59
[s_id] => 23
[m_id] =>
[title] => kkkkkkkk kkkk
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => xxxx xxx xxx
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 112
[s_id] => 37
[m_id] =>
[title] => lllllllllll llllll
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 113
[s_id] => 38
[m_id] =>
[title] => mmmmmmm mmmm
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 123
[s_id] => 39
[m_id] =>
[title] => nnnnnnnnn hhhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 456
[s_id] => 18
[m_id] =>
[title] => ppppppp pppppp
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 545
[s_id] => 19
[m_id] =>
[title] => qqqqqqqqq qqq
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 435
[s_id] => 20
[m_id] =>
[title] => ssssssssss sssssssssss
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
)
)
)
There might be N nesting levels in this array.
Now for each array element, if ['type'] is G then I want to remove keys [s_id] and [m_id] and for any other type I want to remove keys [start], [end] and [time].
Edit : I have tried using a recursive function but it is not giving the desired result.
private function getarray(array &$arr){
foreach ($arr as $item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
foreach ($item["children"] as $c)
{
$child = array();
getarray($child);
}
}
}
How can I achieve this?
Thanks.
function filterMyArray($arr){
if(isset($arr['type'] && $arr['type'] == 'G'){
unset($arr['s_id']);
unset($arr['m_id']);
}
else if(isset($arr['type']){
unset($arr['start']);
unset($arr['end']);
unset($arr['time']);
}
if(isset($arr['children'])
$arr['children'] = filterMyArray($arr['children']);
return $arr;
}
This function is not tested, I wrote it from memory.
But seriously, Googling something like that doesn't hurt. It would take you 5 minutes to get all the required knowledge to do this...
Resolved it myself :
private function getarray(array &$arr){
foreach ($arr as &$item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
if (is_array($item['children']) && sizeof($item['children']) > 0){
getarray($item['children']);
}
}
}
And this is working fine. I have posted this answer to help other people who are novice to php and trying to overcome their problem.
And this is a slap to the jealous people who did not helped me but asked "What did you tried so long?" with a superiority complex attitude. I did it myself.

cakephp access array in view

I have a array looks like this:
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 3
[title] => Title001
[created] => 0000-00-00 00:00:00
[modified] => 2013-08-05 17:39:07
)
[Keyword] => Array
(
[0] => Array
(
[id] => 1
[project_id] => 3
[title] => Num1
[demand] => 50000000000
[competition] => 37889.56700
[cpc] => 676.50
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:25
)
[1] => Array
(
[id] => 13
[project_id] => 3
[title] => test
[demand] => 314
[competition] => 2341.00000
[cpc] => 9999.99
[created] => 2013-09-16 11:05:12
[modified] => 2013-09-16 11:05:12
)
)
)
[1] => Array
(
[Project] => Array
(
[id] => 4
[title] => Erdmann
[created] => 0000-00-00 00:00:00
[modified] =>
)
[Keyword] => Array
(
[0] => Array
(
[id] => 3
[project_id] => 4
[title] => Num5
[demand] => 76534000000
[competition] => 5555.55560
[cpc] => 99.34
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:36
)
[1] => Array
(
[id] => 4
[project_id] => 4
[title] => Num anything
[demand] => 84000
[competition] => 8765.62340
[cpc] => 543.83
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-11 12:08:50
)
)
)
[2] => Array
(
[Project] => Array
(
[id] => 5
[title] => Value
[created] => 2013-09-11 11:18:22
[modified] => 2013-09-11 11:18:22
)
[Keyword] => Array
(
[0] => Array
(
[id] => 2
[project_id] => 5
[title] => foo
[demand] => 4500000000
[competition] => 876.78500
[cpc] => 66.67
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:32
)
[1] => Array
(
[id] => 5
[project_id] => 5
[title] => bar
[demand] => 5000568
[competition] => 667.56543
[cpc] => 667.60
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-11 12:08:09
)
)
)
[3] => Array
(
[Project] => Array
(
[id] => 6
[title] => wolrd.net
[created] => 2013-09-16 11:23:42
[modified] => 2013-09-16 11:23:42
)
[Keyword] => Array
(
[0] => Array
(
[id] => 15
[project_id] => 6
[title] => Earth
[demand] => 5363
[competition] => 93479.34000
[cpc] => 5599.99
[created] => 2013-09-16 11:25:15
[modified] => 2013-09-16 11:26:15
)
)
)
)
What is the right way to access Title001 with all Keywords?

Categories