dongwu3596 2012-05-04 10:18
浏览 73
已采纳

在php中将master数组的相同值与slave数组相结合

I have one master array and multi slave arrays and i need function that replace values on same array keys.

example:

master array:

$master = array(**"a"=>"master a"**,**"b"=>"master b"** "c"=>"master c");

slave arrays:

$slave_a = array("s1"=>array(**"a"=>"slave a"**,"f"=>"slave f"));
$slave_b = array("s2"=>"slave" **"b"="slave b"**);

I need function to filter slave arrays to look like:

$slave_a to be array("s1"=>array(**"a"=>"master a"**,"f"=>"slave f"));
$slave_b to be array("s2"=>"slave" **"b"="master b"**);

Тhanks in advance and sorry for my poor English

  • 写回答

1条回答 默认 最新

  • dswmmvrg40957 2012-05-04 11:36
    关注

    OK, so after parsing the ini files using parse_ini_file You should end up with arrays similar to this (when using process sections TRUE):

    $first_ini = array(
        'MAIN' => array(
            'lib' => 'lib_folder',
            'bin' => 'bin_folder',
            'usr' => 'usr_folder',
            'etc' => 'etc_folder'
        )
    );
    
    $second_ini = array(
        'alib' => 1,
        'abin' => 0,
        'ausr_other' => 'usr/tree'
    );
    
    $master_ini = array(
        'lib' => 'lib_other',
        'abin' => 1
    );
    

    Then You can use this simple function (not tested):

    function combine_master_with_slave($master, $slave) {
        foreach($slave as $key => $val) { // we will loop through slave array
            if(is_array($val)) { // if the value is an array use recursion
                $slave[$key] = combine_master_with_slave($master, $val);
            }
            if(isset($master[$key])) { // if master array contains the same key as a slave array, replace it's value by the master's one
                $slave[$key] = $master[$key];
            }
        }
        return $slave; // finaly return the processed slave array
    }
    

    But I am really not sure whether this will work, maybe it will need some slight modifications...

    Also You will have to call this function so many times as many slave arrays You have, or in other words, You will have to call this function for each slave array.

    This should work for any sub-array level, even it would be [ [ [ [ [ [][][][] ] ] ] ] ].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退