Make a sub array based on main array value - php

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

Related

array format issue PHP

I want to change an array format according to my needs. When I fetch data from a database using cakephp find('all') method, it returns something that is not in the format that I expected.
My resultant array is:
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-20 12:25:34
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
[zip_code_id] => 42
[created] => 2015-05-20 12:37:12
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 42
[created] => 2015-05-20 12:37:28
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[3] => Array
(
[DriverLocation] => Array
(
[id] => 26
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 43
[created] => 2015-05-20 12:43:59
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[4] => Array
(
[DriverLocation] => Array
(
[id] => 41
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 6
[created] => 2015-05-21 05:23:53
)
[ZipCode] => Array
(
[id] => 6
[province_id] => 3846
[city] => Whittier
[zip_code] => 90607
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
)
And I want to convert it into something like this:
Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 15478
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
[1] => 15478
[2] => 90607
)
[city] => Array
(
[0] => Rohtak
[1] => Rohtak
[2] => Whittier
)
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
Filter according to DriverLocation->driver_id
Seems that these arrays are exactly the same, except that you put every "record" of DriverLocation in its own variable. The idea here is that you receive the data from the DriverLocation Model in the fictional controller and put it in a variable; for example:
$allDriverLocations = $this->DriverLocation->find("all");
Now $allDriverLocations contains the first one of your mentioned arrays.
Next step is to pass it to the view:
$this->set(compact("allDriverLocations"));
And now to create a table in the associated view you should iterate through $allDriverLocations:
<table>
<tbody>
<?php
// Loop through the array with a foreach
foreach($allDriverLocations as $driverLocation){
// Create the table row here using the HtmlHelper.
// If you want to reach "Rohtak" for example, you use $driverLocation["Zipcode"]["city"] to print it.
}
?>
</tbody>
</table>
I wouldn't know why you should not follow Cake's easy conventions...

Convert flat PHP array to multidimensional array

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.

CakePHP Threaded "Find" with Associated Models

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

Getting the value of first array from the array

In php I have an array look like this.
Array ( [0] =>
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
[2] => Array ( [id] => 12 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
[3] => Array ( [id] => 14 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
)
From here I want to get the first value of array. For example I want to get the value of first array
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
So can someone kindly tell me how to get the value of 1st array?Any help and suggestions will be really appreciable. Thanks
say all your array was in a variable $myArray, then
myArray[1]
will give you your first array

Merging array for parent category in Cakephp

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.

Categories