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
        )

)
3
  • And your question is? Commented Feb 4, 2014 at 17:21
  • I think that you can find the solution here stackoverflow.com/questions/15524419/… Commented Feb 4, 2014 at 18:00
  • Try $array + $array Commented Feb 4, 2014 at 18:10

3 Answers 3

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];
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

What's the problem with using strings as keys??? This is what makes associative arrays >> traditional arrays.
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 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
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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.