我应该使用哪种PHP数组函数?

时间:2011-06-29 14:17:31

标签: php multidimensional-array

我有两个数组:

Array
(
[0] => Array
    (
        [id] => 1
        [type] => field
        [remote_name] => Title
        [my_name] => title
        [default_value] => http%3A%2F%2Ftest.com
    )

[1] => Array
    (
        [id] => 2
        [type] => field
        [remote_name] => BookType
        [my_name] => book-type
        [default_value] => 
    )

[2] => Array
    (
        [id] => 3
        [type] => value
        [remote_name] => dvd-disc
        [my_name] => dvd
        [default_value] => 
    )
)


Array
(
[title] => Test
[book-type] => dvd
)

我需要获取第二个数组中的每个键,将其与第一个数组中的my_name值匹配,并将其替换为第一个数组的相应remote_name值,同时保留第一个数组的值第二阵列。

必须有一些c array zy功能才能提供帮助!

编辑:还有一些情况,第二个数组的value需要被第一个数组的value remote_name替换,其中第二个数组的值为my_name数组匹配第一个数组book-type => dvd的值。我怎样才能做到这一点?

EG:BookType => dvd-disc应该变为{{1}}

3 个答案:

答案 0 :(得分:2)

像这样?:

$first = array(
    array(
        'id' => 1,
        'type' => 'field',
        'remote_name' => 'Title',
        'my_name' => 'title',
        'default_value' => 'http%3A%2F%2Ftest.com',
    ),
    array(
        'id' => 2,
        'type' => 'field',
        'remote_name' => 'BookType',
        'my_name' => 'book-type',
        'default_value' => '',
    ),
    array(
        'id' => 3,
        'type' => 'value',
        'remote_name' => 'dvd-disc',
        'my_name' => 'dvd',
        'default_value' => '',
    ),
);

$second = array(
    'title' => 'Test',
    'book-type' => 'dvd',
);

$map = array('fields' => array(), 'values' => array());
foreach ($first as $entry) {
    switch ($entry['type']) {
        case 'field':
            $map['fields'][$entry['my_name']] = $entry['remote_name'];
            break;

        case 'value':
            $map['values'][$entry['my_name']] = $entry['remote_name'];
            break;
    }
}

$new = array();
foreach ($second as $key => $val) {
    $new[isset($map['fields'][$key]) ? $map['fields'][$key] : $key] = isset($map['values'][$val]) ? $map['values'][$val] : $val;
}

print_r($new);

<强>输出

Array
(
    [Title] => Test
    [BookType] => dvd-disc
)

<强>解释

第一个循环收集字段和值的my_name / remote_name对,并使它们更易于访问。 像这样:

Array
(
    [fields] => Array
        (
            [title] => Title
            [book-type] => BookType
        )

    [values] => Array
        (
            [dvd] => dvd-disc
        )

)

第二个循环将遍历$second并使用其中的键/值对填充$new。但这样做会检查$map中的键/值重复项。

地图中未找到的键或值将

答案 1 :(得分:0)

foreach($arr1 as &$el) {
    $el['remote_name'] = $arr2[$el['my_name']];
}
unset($el);

答案 2 :(得分:0)

我不知道这样的c array zy函数,但我知道你怎么做:

//$array1 is first array, $array2 is second array

foreach($array1 as $key => $value){
    if (isset($value['remote_name'], $value['my_name']) && $value['remote_name'] && $value['my_name']){
        $my_name = $value['my_name'];
        if (isset($array2[$my_name])) {
            $remote_name = $value['remote_name'];
            $array2[$remote_name] = $array2[$my_name];
            //cleanup
            unset($array2[$my_name]);
        }
    }
}