0

I have some code in cake php

$find_parent_category = $this->Project->Category->find('list',array(
    'fields' => array('Category.Category','id'),
    'order' => array('Category.Category'),
    'conditions' => array(
    'Category.id' => $sub_category
    )
));

   pr($find_parent_category)

output

Array
(
    [Accounting & Financial] => 1
    [Data Management] => 7
)



$find_sub_category = $this->Project->Category->find('list',array(
    'fields' => array('Category.Category','id','parent_id'),
    'order' => array('Category.Category'),
    'conditions' => array(
    'Category.parent_id' => $sub_category
    )
));

   pr($find_sub_category)

output

Array
(
    [1] => Array
        (
            [Accounting Audit & Assurance] => 24
            [Accounting Services] => 25
            [Accounting Software] => 26
        )

    [7] => Array
        (
            [Analytical Tools] => 27
            [Cloud Computing] => 28
            [Data Bases] => 29
        )

)

And i need output like that

Array
(
    [Accounting & Financial] => Array
        (
            [Accounting Audit & Assurance] => 24
            [Accounting Services] => 25
            [Accounting Software] => 26
        )

    [Data Management] => Array
        (
            [Analytical Tools] => 27
            [Cloud Computing] => 28
            [Data Bases] => 29
        )

)
thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67

3 Answers3

1

No need to merge arrays, you already have two arrays that are related so just loop through the array that maps the key you want to the actual key and create a new array like this:

$new_array = array();

foreach($find_parent_category as $key => $elm)
{
    $new_array[$key] = $find_sub_category[$elm];
}
elitechief21
  • 2,964
  • 1
  • 17
  • 15
1

Using strings as keys like that will eventually get you into trouble. You are better off leaving the numerical keys in $find_sub_category, and flipping the output of the original $find_parent_category query to this:

'fields' => array('id','Category.Category'),

so that your $find_parent_category looks like this

Array
(
    [1] => Accounting & Financial
    [7] => Data Management
)

this letting you use it as a lookup table to resolve those top level keys when using $find_sub_category

Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50
  • What's the problem with using strings as keys??? This is what makes associative arrays >> traditional arrays. – elitechief21 Feb 04 '14 at 17:43
  • Not saying strings are bad, but when pulling from a database, you may eventually get characters as in your keys that you didnt expect, like possible [UTF8 issues](http://stackoverflow.com/questions/10696067) issues that may or may not exist in your build, also the ID is more likely to be unique, where what happens if you have the same category name in 2 departments and one of them gets overridden – Uberfuzzy Feb 04 '14 at 17:49
  • Character encoding is valid concern but so long as everything is set up correctly on the database side (primary key constraints included) than neither encoding or uniqueness should be an issue – elitechief21 Feb 04 '14 at 18:18
0

I have merged array base on same key.......

$find_parent_category = $this->Project->Category->find('list',array(
           'fields' => array('Category.id','Category'),
           'order' => array('Category.Category'),
           'conditions' => array(
           'Category.id' => $sub_category
           )
       ));



$find_sub_category = $this->Project->Category->find('list',array(
           'fields' => array('Category.Category','id','parent_id'),
           'order' => array('Category.Category'),
           'conditions' => array(
           'Category.parent_id' => $sub_category
           )
       ));

}





function merge_common_keys(){
    $arr = func_get_args();
    $num = func_num_args();

    $keys = array();
    $i = 0;
    for($i=0;$i<$num;++$i){
        $keys = array_merge($keys, array_keys($arr[$i]));
    }
    $keys = array_unique($keys);

    $merged = array();

    foreach($keys as $key){
        $merged[$key] = array();
        for($i=0;$i<$num;++$i){
        $merged[$key][] = isset($arr[$i][$key])?$arr[$i][$key]:null;
        }
    }
    return $merged;
    }

$merged = merge_common_keys($find_parent_category,$find_sub_category);


pr($merged);

Click see demo here

thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67