duangou6446 2011-04-07 21:07
浏览 29
已采纳

帮助在php中组合2个多维数组

I have been a long time reader here and never posted but I was wondering if somebody could help me out with a problem I am having...

I have 2 arrays which print like this:

$fields = Array
(
    [0] => Array
        (
            [field] => NewField
            [label] => New Field
            [required] => 1
        )

    [1] => Array
        (
            [field] => _Field2
            [label] => Field 2
            [required] => 0
        )

)



$fields_data = Array
(
    [0] => Array
        (
            [field_id] => 1
            [field_name] => New Field
            [db_name] => NewField
            [field_type] => text
        )

    [1] => Array
        (
            [field_id] => 6
            [field_name] => Field 2
            [db_name] => _Field2
            [field_type] => text
        )

)

And I need to create an array which looks like this :

$new_arr = Array
(
    [NewField] => Array
        (
            [field_id] => 1
            [field_name] => New Field
            [field_type] => text
            [label] => New Field
            [required] => 1
        )

    [_Field2] => Array
        (
            [field_id] => 6
            [field_name] => Field 2
            [field_type] => text
            [label] => Field 2
            [required] => 0
        )

)

So what happens is the db_name become the array keys and then the 2 array where the field matches the db_name combine into one array.

Here is some code I have been trying to work with but obviously it is not working for whatever reason (PHP isn't my strong point):

foreach($fields as $field){

    $new_arr[$field['field']] = array();

foreach($fields_data as $tkey => $tval) {


if($fields_data[$tkey]['db_name'] = $fields['field']) {

$new_arr[$field['field']]['field_id'] = $fields_data[$tkey]['field_id'];

    }
    }

    }

Obviously the above code would only create the field_id element in the array however as it is it isn't working. I think having the 2 loops is overwriting the field_id value each time. But as I said php is not my strong point so any help on this would be much appreciated.

Thanks

  • 写回答

3条回答 默认 最新

  • duanchun1909 2011-04-07 22:34
    关注

    This code will take the two arrays you're given and turn them into the third. Hope it helps! Could probably use some tweaking, but hopefully it solves your problem.

    $new_arr = array();
    foreach( $fields as $field )
    {
        foreach( $fields_data as $field_data )
        {
            if( $field['field'] == $field_data['db_name'] )
            {
                $new_arr[ $field['field'] ] = array_merge( $field, $field_data );
                unset( $new_arr[$field['field']]['field'], $new_arr[$field['field']]['db_name'] );
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM