I have two arrays first array is main comment and second array is sub comment.
1st Array
Array
(
[0] => Array
(
[id] => 1
[comment] => Nice Blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
[cnt] =>
)
[1] => Array
(
[id] => 2
[comment] => Awesome blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
[cnt] => 2
)
)
2nd Array
Array
(
[0] => Array
(
[id] => 3
[comment] => Yes, Really good
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
)
[1] => Array
(
[id] => 4
[comment] => Thank you
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
)
)
Now we need an array like this with a single array based on "parent_id" and "id":
I want new array like this :
Array
(
[0] => Array
(
[id] => 1
[comment] => Nice Blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
[cnt] =>
)
[1] => Array
(
[id] => 2
[comment] => Awesome blog
[parent_id] => 0
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
[cnt] => 2
[sub_comment] => Array
(
[0] => Array
(
[id] => 3
[comment] => Yes, Really good
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Shaishav Desai
[image_path] => 067da3ff3b891981caa5b5d98c44052c.png
)
[1] => Array
(
[id] => 4
[comment] => Thank you
[parent_id] => 2
[created] => 2018-02-20 00:00:00
[user_title] => Lead1 Assistant
[image_path] => 099dbe4c58606a3abf867d821e62fdf9.png
)
)
)
)
So I can easily know this comment have sub comment and display based on new array.
Would you please let me know is it possible or not ?
Thanks in advance.
// Suppose $array1 is firts array;
// $array2 is second array
foreach($array2 as $subary){
$cnt=0;
foreach($array1 as $key=> $mainary){
if($subary['parent_id']==$mainary['id']){
$array1[$key]['sub_comment']=$subary;
}
}
}
print_r($array1);
I am attempting to json_encode an array in php5.3.
json_encode($paperwork_info[0])
The result is bad json. The array is as follows:
[paperwork_guid] => c5bfe512-908d-c513-5a5e-e3a2fbb5548b
[name] => recycle
[sections] => Array
(
[0] => header
[1] => customer
[2] => scope_bullets
[3] => product
[4] => signature
[5] => installer
[6] => order_data
)
[data] => Array
(
[header] => Array
(
[image] => Array
(
[src] => recycle.png
[format] => float:left
)
)
[customer] => Array
(
[f_name] => TEST ONLY
[l_name] => NEIMEIER
[middle_name] => none
[title] => none
[address1] => 28 OEHMAN BLVD
[address2] =>
[address3] =>
[zip] => 14225
[city] => BUFFALO
[state_abbrev] => NY
[email_address] => DALE.NEIMEIER#INSTALLS.COM
[phones] => Array
(
[alt] =>
[cell] =>
[work] =>
[home] => 7165551212
)
)
[scope_bullets] => Array
(
[sku_3001] => Array
(
[bullet_0] => Array
(
[paperwork_bullet_obj_guid] => 2ebefa96-6f6b-069e-e194-245d138b9845
[ordering] => 1
[bullet_text] => This is bullet point #1
[child_level] => 1
[formatting] => Array
(
[0] => Array
(
[start_word] => 2
[num_words] => 0
[tag] => b
[href] =>
)
[1] => Array
(
[start_word] => 2
[num_words] => 1
[tag] => a
[href] => http://www.cnn.com
)
)
)
[bullet_1] => Array
(
[paperwork_bullet_obj_guid] => 734db3f4-01a0-b025-9624-cc52a1845dff
[ordering] => 1
[bullet_text] => Sub-point #1.1
[child_level] => 2
)
[bullet_2] => Array
(
[paperwork_bullet_obj_guid] => ebf5ef02-906e-2005-e499-27eae2edefe9
[ordering] => 1
[bullet_text] => Sub point #1.1.1
[child_level] => 3
)
[bullet_3] => Array
(
[paperwork_bullet_obj_guid] => 447997c1-fd9c-25be-b9bf-39257009fb8b
[ordering] => 1
[bullet_text] => This is bullet point #2
[child_level] => 1
)
[bullet_4] => Array
(
[paperwork_bullet_obj_guid] => 5def2d9c-d322-788c-0afe-d13707004b97
[ordering] => 1
[bullet_text] => Sub point #2.1
[child_level] => 2
)
[bullet_5] => Array
(
[paperwork_bullet_obj_guid] => 84936151-65a3-bcca-951f-b69ff16d34ec
[ordering] => 2
[bullet_text] => Sub point #2.2
[child_level] => 2
)
)
[sku_4405] =>
)
[product] => Array
(
[0] => Array
(
[model] => Paperwork Test abc123
[serial] => 777333
[weight] => 26
[size] =>
[width] =>
[height] => 32
[num_boxes] => 1
[product_name] =>
[sku] =>
[model_number] =>
[part_number] =>
[serial_number] =>
[cat_value] => Best Buy Equipment
[product] =>
[product_type] =>
[product_size] =>
[category] =>
)
)
[signature] => Array
(
[date] => 2016-06-15
[need_customer] => 1
[need_tech] => 1
[need_store] =>
[need_warehouse] =>
)
[installer] => Array
(
[anum] => 45455
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[phone] => (317) 519-0481
[tech_f_name] =>
[tech_l_name] =>
[company_name] => ZICO LLC
[email_address] => ZICOLLC#GMAIL.COM
)
[order_data] => Array
(
[assigned_date] => 2016-06-07 22:24:47-04
[purchased_date] => 2016-06-06
[creation_date] => 2016-06-06
[install_date] => 2016-06-10
[install_time] => 19:00:00
[prescheduled_date] => 1969-12-31
[prom_start_time] => 2016-06-10 08:06:00
[prom_end_time] => 2016-06-10 11:06:00
[client_order_id] => 12365478996325412
[job_num] => 4043269
[campaign_wkord_obj_guid] => a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0
[pos_obj_guid] => 096e38c2-55c3-80cf-0778-23f81f1cf2f6
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[client_obj_guid] => 247e893a-3ea4-c544-47b2-f23ff16017c6
[ord_obj_guid] => ae55e034-d7d0-5d13-3dd3-22f99df8ead4
[skus] => Array
(
[0] => Array
(
[job_type_obj_guid] => 8234ca2c-e40e-c48c-befc-7ceac5e9de32
[job_sku] => 3001
[client_sku] =>
[sku_name] => Site Survey
)
[1] => Array
(
[job_type_obj_guid] => a3f60c3b-ad3c-d828-9898-fa200edcd3cd
[job_sku] => 4405
[client_sku] =>
[sku_name] => Home Delivery
)
)
[client_specific_data] =>
)
)
)
The resultant json string is as follows:
{"paperwork_guid":"c5bfe512-908d-c513-5a5e-e3a2fbb5548b","name":"recycle","sections":{"0":"header","1"
:"customer","2":"scope_bullets","3":"product","4":"signature","5":"installer","6":"order_data"},"data"
:{"header":{"image":{"src":"recycle.png","format":"float:left"}},"customer":{"f_name":"TEST ONLY","l_name"
:"NEIMEIER","middle_name":"none","title":"none","address1":"28 OEHMAN BLVD","address2":"","address3"
:null,"zip":"14225","city":"BUFFALO","state_abbrev":"NY","email_address":"DALE.NEIMEIER#INSTALLS.COM"
,"phones":{"alt":"","cell":"","work":"","home":"7165551212"}},"scope_bullets":{"sku_3001":{"bullet_0"
:{"paperwork_bullet_obj_guid":"2ebefa96-6f6b-069e-e194-245d138b9845","ordering":"1","bullet_text":"This
is bullet point #1","child_level":"1","formatting":{"0":{"start_word":"2","num_words":"0","tag":"b"
,"href":""},"1":{"start_word":"2","num_words":"1","tag":"a","href":"http:\/\/www.cnn.com"}}},"bullet_1"
:{"paperwork_bullet_obj_guid":"734db3f4-01a0-b025-9624-cc52a1845dff","ordering":"1","bullet_text":"Sub-point
#1.1","child_level":"2"},"bullet_2":{"paperwork_bullet_obj_guid":"ebf5ef02-906e-2005-e499-27eae2edefe9"
,"ordering":"1","bullet_text":"Sub point #1.1.1","child_level":"3"},"bullet_3":{"paperwork_bullet_obj_guid"
:"447997c1-fd9c-25be-b9bf-39257009fb8b","ordering":"1","bullet_text":"This is bullet point #2","child_level"
:"1"},"bullet_4":{"paperwork_bullet_obj_guid":"5def2d9c-d322-788c-0afe-d13707004b97","ordering":"1","bullet_text"
:"Sub point #2.1","child_level":"2"},"bullet_5":{"paperwork_bullet_obj_guid":"84936151-65a3-bcca-951f-b69ff16d34ec"
,"ordering":"2","bullet_text":"Sub point #2.2","child_level":"2"}},"sku_4405":null},"product":{"0":{"model"
:"Paperwork Test abc123","serial":"777333","weight":"26","size":null,"width":null,"height":"32","num_boxes"
:"1","product_name":null,"sku":null,"model_number":null,"part_number":null,"serial_number":null,"cat_value"
:"Best Buy Equipment","product":null,"product_type":null,"product_size":null,"category":null}},"signature"
:{"date":"2016-06-15","need_customer":true,"need_tech":true,"need_store":false,"need_warehouse":false
},"installer":{"anum":"45455","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","phone":"(317) 519-0481"
,"tech_f_name":null,"tech_l_name":null,"company_name":"ZICO LLC","email_address":"ZICOLLC#GMAIL.COM"
},"order_data":{"assigned_date":"2016-06-07 22:24:47-04","purchased_date":"2016-06-06","creation_date"
:"2016-06-06","install_date":"2016-06-10","install_time":"19:00:00","prescheduled_date":"1969-12-31"
,"prom_start_time":"2016-06-10 08:06:00","prom_end_time":"2016-06-10 11:06:00","client_order_id":"12365478996325412"
,"job_num":"4043269","campaign_wkord_obj_guid":"a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0","pos_obj_guid"
:"096e38c2-55c3-80cf-0778-23f81f1cf2f6","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","client_obj_guid"
:"247e893a-3ea4-c544-47b2-f23ff16017c6","ord_obj_guid":"ae55e034-d7d0-5d13-3dd3-22f99df8ead4","skus"
:{"0":{"job_type_obj_guid":"8234ca2c-e40e-c48c-befc-7ceac5e9de32","job_sku":"3001","client_sku":null
,"sku_name":"Site Survey"},"1":{"job_type_obj_guid":"a3f60c3b-ad3c-d828-9898-fa200edcd3cd","job_sku"
:"4405","client_sku":null,"sku_name":"Home Delivery"}},"client_specific_data":""}}}
It appears as though new line feeds are being inserted randomly throughout the josn string. I am not sure if it is just how firebug is outputting the string, but copying-and-pasting the string into jsonLINT produces errors.
Any help would be appreciated.
I have the following array structure:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
)
[1] => Array
(
[id] => 4
[user_id] => 1
[created] => 2014-11-09 13:52:02
[content] => 456789
[status] => 1
[parent] => 0
[project_id] => 1
)
)
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
[2] => Array
(
[id] => 5
[user_id] => 1
[created] => 2014-11-09 13:52:17
[content] => 8765432
[status] => 1
[parent] => 4
[project_id] => 1
)
)
I would like to merge them into a parent/child relationship so it looks like follows:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
)
)
)
How could I go about doing that with PHP?
Assuming that all ids are unique in your parents array, you can do this:
// build a new array using parent's ID values as the key,
// to simplify searching for parent IDs.
foreach ($parents as $key => $value){
$merged[$value['id']] = $value;
}
foreach ($children as $child){
$parentID = $child['parent'];
if (array_key_exists( $parentID, $merged )) {
// add the child array to its parent's ['children'] value
$merged[$parentID]['children'][] = $child;
}
}
In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.
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?
I am using a $this->find('threaded') in my CakePHP application and am trying to pull in an associated model within the find (HABTM). I have tried several methods, such as 'joins', 'recursive' and 'contains' all with no luck. I am using CakePHP 2.3.6
Here is my (working) code.
class EventsController extends AppController {
public function promoters($id = null) {
$options = array('conditions' => array('Event.id' => $id));
$event = $this->Event->find('first', $options);
$this->set('event', $event);
$this->loadModel('EventsPromoter');
$treelistConditions = array(
'conditions' => array(
'event_id' => $id
),
);
$promoterTree = $this->EventsPromoter->find('threaded', $treelistConditions);
$this->set('promoters', $promoterTree);
}
}
This results in the following array output
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[children] => Array
(
)
)
)
)
)
What I would like my code to output is the following
Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 1
[promoter_id] => 1
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 1
[first_name] => 'Bob'
[last_name] => 'Smith'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 10
[promoter_id] => 4
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 4
[first_name] => 'Sally'
[last_name] => 'Sue'
)
[children] => Array
(
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 13
[promoter_id] => 6
[event_id] => 1
[parent_id] => 1
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 6
[first_name] => 'Ben'
[last_name] => 'King'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 2
[promoter_id] => 2
[event_id] => 1
[parent_id] =>
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 2
[first_name] => 'Jack'
[last_name] => 'Sparrow'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 11
[promoter_id] => 5
[event_id] => 1
[parent_id] => 2
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 5
[first_name] => 'Jane'
[last_name] => 'Doe'
)
[children] => Array
(
[0] => Array
(
[EventsPromoter] => Array
(
[id] => 6
[promoter_id] => 3
[event_id] => 1
[parent_id] => 11
[created] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
)
[Promoter] => Array
(
[promoter_id] => 3
[first_name] => 'Mike'
[last_name] => 'Jones'
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[EventsPromoter] => Array
(
[id] => 14
[promoter_id] => 7
[event_id] => 1
[parent_id] => 2
[created] => 2013-07-26 00:30:09
[modified] => 2013-07-26 00:30:09
)
[Promoter] => Array
(
[promoter_id] => 7
[first_name] => 'Spider'
[last_name] => 'Man'
)
[children] => Array
(
)
)
)
)
)
Nothing I have been trying is working, and this seems like something Cake should be able to handle fairly easily. Thanks in advance for any help!
I was taking a slightly wrong approach for what I was trying to accomplish. While I still think this should be possible from my EventsController class, I elected to make it in the EventsPromotersController HABTM class. The code is much simpler using this method
class EventsPromotersController extends AppController {
public function event($event_id = null) {
$options = array('conditions' => array('event_id' => $event_id));
$promoters = $this->EventsPromoter->find('threaded', $options);
$this->set('promoters', $promoters);
}
}