将一个数组值与另一个数组值匹配并检索/替换匹配的值

时间:2015-04-08 19:41:30

标签: php arrays

我正在尝试使用$ arrayA中的“locationid”键检索$ arrayB中的“location”:

$arrayA=Array
(
    (0) => Array
        (
            (domain) => 'testing.com',
            (locationid) => '7',
            (description) => 'Lorem'
        ),

    (1) => Array
        (
            (domain) => 'testing2.com',
            (locationid) => '6',
            (description) => 'Ipsum'
        ),

    (2) => Array
        (
            (domain) => 'testing3.com',
            (locationid) => '1',
            (description) => 'Foo'
        )
);

$arrayB=Array
(
    (0) => Array
        (
            (locationid) => '1',
            (location) => 'London'
        ),

    (1) => Array
        (
            (locationid) => '6',
            (location) => 'New York'
        ),

    (2) => Array
        (
            (locationid) => '7',
            (location) => 'Tokyo'
        )
);

然后最终以$ arrayC结束,这将是:

$arrayC=Array
(
    (0) => Array
        (
            (domain) => 'testing.com',
            (location) => 'Tokyo',
            (description) => 'Lorem'
        ),

        etc...
);

最好的方法是什么?我想某种“foreach”功能,但我无法理解它!

非常感谢

3 个答案:

答案 0 :(得分:0)

$arrayB = array( 7 => 'Tokyo' );
$arrayC = array_map( function(&$a) use ($arrayB) {
    $a['location'] = $arrayB[$a['locationid']];
    unset([$a['locationid']);
    return $a; 
}, $arrayA);

也许就是这样。

答案 1 :(得分:0)

$arrayA = array(
array('domain' => 'testing.com', 'locationid' => '7', 'description' => 'Lorem'),
array('domain' => 'testing2.com', 'locationid' => '6', 'description' => 'Ipsum'),
array('domain' => 'testing3.com', 'locationid' => '1', 'description' => 'Foo')
);

$arrayB = array(
array('locationid' => '1', 'location' => 'London'),
array('locationid' => '6', 'location' => 'New York'),
array('locationid' => '7', 'location' => 'Tokyo')
);

$arrayA_a = array();
foreach($arrayA AS $arrayA_b)
{
    $arrayA_a["$arrayA_b[locationid]"] = $arrayA_b;
}

$arrayB_a = array();
foreach($arrayB AS $arrayB_b)
{
    $arrayB_a["$arrayB_b[locationid]"] = $arrayB_b;
}

$arrayC = array();
foreach($arrayA_a AS $arrayA_a_key => $arrayA_a_value)
{
    $arrayC["$arrayA_a_key"]['domain']      = $arrayA_a_value['domain'];
    $arrayC["$arrayA_a_key"]['description'] = $arrayA_a_value['description'];
    $arrayC["$arrayA_a_key"]['location']    = $arrayB_a["$arrayA_a_key"]['location'];
}

print_r($arrayC);
Array
(
[7] => Array
    (
        [domain] => testing.com
        [description] => Lorem
        [location] => Tokyo
    )

[6] => Array
    (
        [domain] => testing2.com
        [description] => Ipsum
        [location] => New York
    )

[1] => Array
    (
        [domain] => testing3.com
        [description] => Foo
        [location] => London
    )

)

如果要重置$ arrayC的键:

$arrayC = array_merge($arrayC);
Array
(
[0] => Array
    (
        [domain] => testing.com
        [description] => Lorem
        [location] => Tokyo
    )

[1] => Array
    (
        [domain] => testing2.com
        [description] => Ipsum
        [location] => New York
    )

[2] => Array
    (
        [domain] => testing3.com
        [description] => Foo
        [location] => London
    )

)

答案 2 :(得分:0)

一个嵌套循环可以做 - 但我认为这更具可读性

<?php
$arrayA=Array
(
    '0' => Array
        (
            'domain' => 'testing.com',
            'locationid' => '7',
            'description' => 'Lorem'
        ),

    '1' => Array
        (
            'domain' => 'testing2.com',
            'locationid' => '6',
            'description' => 'Ipsum'
        ),

    '2' => Array
        (
            'domain' => 'testing3.com',
            'locationid' => '1',
            'description' => 'Foo'
        )
);

$arrayB=Array
(
    '0' => Array
        (
            'locationid' => '1',
            'location' => 'London'
        ),

    '1' => Array
        (
            'locationid' => '6',
            'location' => 'New York'
        )
);

function getDomain($id, $list) {
  foreach ($list as $domain) {
    if ($domain['locationid'] == $id) {
      return $domain;
    }
  }
}

$arrayC = array();
foreach ($arrayB as $id) {
  $newData = getDomain($id['locationid'], $arrayA);
  $newData['location'] = $id['location'];
  $arrayC[] = $newData;
}

var_dump($arrayC);