php - show job title inside category but same category dont repeat -


the following code:

$services = $this->find('all', array(             'contain' => array('category','country'),             'joins' => array(                  array('table' => 'services_to_categories',                     'alias' => 'servicecategory',                     'type' => 'inner',                     'conditions' => array(                         'servicecategory.service_id = service.id'                     )                 )             ),             'group' => 'service.id'         ));         return $services; 

gives me output:

    array     (         [0] => array             (                 [service] => array                     (                         [id] => 6                         [job_title] => director                      )                 [category] => array                     (                         [0] => array                             (                                 [id] => 1                                 [category] => accounting & financial                             )                         [1] => array                             (                                 [id] => 3                                 [category] => awards & incentives                             )                         [2] => array                             (                                 [id] => 7                                 [category] => data management                             )                     )             )         [1] => array             (                 [service] => array                     (                         [id] => 11                         [job_title] => d                      )                 [category] => array                     (                         [0] => array                             (                                 [id] => 7                                 [category] => data management                             )                         [1] => array                             (                                 [id] => 10                                 [category] => internet services                             )                     )             )         [2] => array             (                 [service] => array                     (                         [id] => 12                          [job_title] => e                     )                 [category] => array                     (                         [0] => array                             (                                 [id] => 4                                 [category] => business consulting                             )                         [1] => array                             (                                 [id] => 7                                 [category] => data management                             )                     )             )         ) 

i need output below:

accounting & financial job_title: director   awards & incentives job_title: director  data management job_title: director  job_title: d job_title: e  internet services job_title: d  business consulting job_title: e 

how 1 use array variable $services in above output?

basically, did loop through array , make new array category key , job title array of values. looped through new array , spit out key , values.

<?php  $category_array = array();   // loop through main array , restructure foreach ($start_array $array_element) {     foreach ($array_element['category'] $category) {         $category_array[$category['category']][] = $array_element['service']['job_title'];     } }   // loop through our new array , print out key , values foreach ($category_array $department => $job_title_array) {     print $department;     foreach ($job_title_array $job_title) {         print "\njob_title: ".$job_title;     }     print "\n\n"; } 

here working demo


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -