2

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] => 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
)

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

3
  • +1 because you're so damn p(h)unny Commented Jun 29, 2011 at 14:20
  • 6
    How can you replace a value in the second array while preserving the value of the second array? Abstraction overflow. Commented Jun 29, 2011 at 14:21
  • 1
    to clarify - I want to basically replace the key of the second array with the remote_name value of the first array that contains the second array's key as the first array's my_name value.............clarified. Commented Jun 29, 2011 at 14:28

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

hurts my brain, but looks pretty good. how would you approach adding it to a new results array instead of modifying the second array?
@Dave Kiss See the updated example. Not much changed, just using $new as container for the data.
actually, are you still around? I have one more loop to throw in this question
@Dave Kiss Is it correct to assume that [type] => value entries should overwrite values and [type] => field entries, the keys?
@Yoshi your assumptions are confirmed
|
0
foreach($arr1 as &$el) {
    $el['remote_name'] = $arr2[$el['my_name']];
}
unset($el);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.