合并基于相同值和密钥PHP的数组

时间:2014-02-04 17:20:28

标签: php arrays cakephp cakephp-2.0

我在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)

输出

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)

输出

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

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

)

我需要那样的输出

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 个答案:

答案 0 :(得分:1)

不需要合并数组,你已经有两个相关的数组,所以只需循环遍历数组,将你想要的键映射到实际的键,然后像这样创建一个新的数组:

$new_array = array();

foreach($find_parent_category as $key => $elm)
{
    $new_array[$key] = $find_sub_category[$elm];
}

答案 1 :(得分:1)

使用字符串作为这样的键最终会让你陷入困境。最好将数字键保留在$find_sub_category中,并将原始$find_parent_category查询的输出翻转为:

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

以便$find_parent_category看起来像这样

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

这使您可以将其用作查找表,以便在使用$find_sub_category时解析这些顶级键

答案 2 :(得分:0)

我在同一个键上合并了数组.......

$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