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

时间:2011-04-07 21:07:54

标签: php arrays loops

我在这里做了很长时间的读者并且从未发布过,但我想知道是否有人可以帮我解决我遇到的问题......

我有2个阵列,打印方式如下:

$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
        )

)

我需要创建一个如下所示的数组:

$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
        )

)

所以会发生什么是db_name成为数组键,然后字段与db_name匹配的2数组组合成一个数组。

以下是我一直试图使用的一些代码,但显然它无论出于何种原因都不起作用(PHP不是我的强项):

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'];

    }
    }

    }

显然,上面的代码只会在数组中创建field_id元素,但因为它不起作用。我认为每次使用2个循环都会覆盖field_id值。但正如我所说,php不是我的强项,所以对此的任何帮助都会非常感激。

谢谢

3 个答案:

答案 0 :(得分:1)

假设你正在合并2个数组,第一个数组中的label和第二个中的field_name相等(但我有一些疑问,因为你在结果数组中同时存在),这是正确的方法。

$newArray = array();
//create a map for o1 complexity
$fieldDataMap = array();
foreach($fields_data as $data){
    $fieldDataMap[$data['field_name']] = $data;
}
//walk over fields to fill newArray
foreach($fields as $field){
    $newArray[$field['field']] = array(
        'field_id' => $fieldDataMap[$field['label']]['field_id'],
        'field_name' => $fieldDataMap[$field['label']]['field_name'],
        'field_type' => $fieldDataMap[$field['label']]['field_type'],
        'label' => $field['label'],
        'required' => $field['required']
    );
}
print_r($newArray);

编辑这个解决方案对于只有2条记录来说似乎有点过分,但是当涉及到100多个大小的数组时,这会因o1的复杂性而受益越来越多。

答案 1 :(得分:0)

假设两个数组具有相同的长度并且元素是有序的:

$newArr = array();
for($i = 0; $i <= count($fields); $i++) {
    $idx = $fields[$i]['field'];
    $newArr[$idx] = array_merge($fields[$i], $fields_data[$i];
}

答案 2 :(得分:0)

此代码将获取您给出的两个数组,并将它们转换为第三个数组。希望能帮助到你!可能会使用一些调整,但希望它能解决你的问题。

$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'] );
        }
    }
}