开发者

Which PHP Array function should I use?

I have two arrays:

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

[1] => Ar开发者_开发技巧ray
    (
        [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
)

I need to take each key in the second array, match it with the my_name value in the first array and replace it with the corresponding remote_name value of the first array while preserving the value of the second array.

There's got to be some carrayzy function to help!

EDIT: There will also be a few cases that the value of the second array will need to be replaced by the value of the first array's remote_name where the value of the second array matches the value of the first array's my_name. How can I achieve this?

EG: book-type => dvd should turn into BookType => dvd-disc


Like so?:

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

Output:

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

Explanation:

The first loop collects the my_name/remote_name pairs for fields and values and makes them more accessible. Like so:

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

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

)

The second loop will traverse $second and use the key/value pairs therein to populate $new. But while doing so will check for key/value duplicates in $map.

Keys or values not found in the map will be used as is.


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


I am not aware of such a carrayzy function, but I know how you could do it:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜