php匹配键并将值复制到新数组

时间:2014-11-01 12:59:17

标签: php multidimensional-array foreach

我有两个数组。一个包含一个地方列表。

$places = Array ([0] => London [1] => New York [2] => Paris [3] => Sydney [4] => Bangkok)

另一个包含很长的地方列表和每个

的颜色
$colours = Array (  [0] => Array ( [0] => Madrid [1] => Blue )  [1] => Array ( [0] => London [1] => Yellow )  [2] => Array ( [0] => Hong Kong [1] => Orange )  [3] => Array ( [0] => Paris [1] => Purple )  [4] => Array ( [0] => Sydney [1] => Pink ))

我正在尝试构建一个最终数组,其中对于$ places中的每个地方,我都有相应的颜色,取自$ colors列表。

我最接近的是

$result = array();
foreach ($places as $x) {
    $result[$x]['colour'] = $colours[$x];
}

但它并没有从$ colors中生成每个位置的颜色。有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:1)

这应该这样做:

$places = Array ("London","New York","Paris", "Sydney","Bangkok");
$colours = Array (  Array ("Madrid", "Blue" ), Array ("London", "Yellow" ), Array ( "Hong Kong", "Orange" ), Array ("Paris", "Purple" ));

$ourArray = Array();
foreach($colours as $place){
      if(in_array($place[0], $places)){
        $ourArray[$place[0]] = $place[1];
      }
}

var_dump($ourArray);

答案 1 :(得分:1)

试试这个

$places = array ('0' => 'London', '1' => 'New York', '2' => 'Paris', '3' => 'Sydney', '4' => 'Bangkok');
$colours = array (  '0' => array ( '0' => 'Madrid', '1' => 'Blue' ),  '1' => array ( '0' => 'London', '1' => 'Yellow' ) , '2' => array ( '0' => 'Hong Kong' ,'1' => 'Orange' ) , '3' => array ( '0' => 'Paris', '1' => 'Purple' ),  '4' => array ( '0' => 'Sydney', '1' => 'Pink' ));

$c = array();
foreach ($colours as $key => $v) {
   $c[$v[0]] = $v[1];

}
$result = array();
foreach ($places as $place) {
 foreach ($c as $k => $cl) {
    if($place == $k)
    $result[$place]['colour'] = $cl;
 }
}
var_dump($result);

答案 2 :(得分:0)

像往常一样,晚会...... :-)有人可能觉得它很有用。

这是一个测试版本(PHP 5.3.18),它使用原始数据结构。对于大型输入数组应该更快,因为它将'place',color'列表转换为具有'place'键的数组,其值为'color'。即你可以直接查找“地方”的颜色。然后,在“地点”的情况下,可以直接“查找”所需的“颜色”。

代码已注释。

<?php // https://stackoverflow.com/questions/26689542/php-match-keys-and-copy-values-to-new-array

$places = array ('0' => 'London', '1' => 'New York', '2' => 'Paris', '3' => 'Sydney', '4' => 'Bangkok');
$colours = array (  '0' => array ( '0' => 'Madrid', '1' => 'Blue' ),  '1' => array ( '0' => 'London', '1' => 'Yellow' ) , '2' => array ( '0' => 'Hong Kong' ,'1' => 'Orange' ) , '3' => array ( '0' => 'Paris', '1' => 'Purple' ),  '4' => array ( '0' => 'Sydney', '1' => 'Pink' ));

$result = array();
foreach ($places as $placeName) {
    $result[] = array( $placeName, placesToColourLookup($placeName));
}

// show output places with colours
var_dump($result);

// end of program

/*
 * @param string Place name
 * @return string Coulour or empty string if not found
 */
function placesToColourLookup($placeName)
{
    global $colours;

    static $colourLookup = null; // only convert the 'place -> colour array once!

    // convert the supplied place -> colour array to an array keyed by 'placeName'.
    // for fast lookup after the first time...
    if (is_null($colourLookup)) {
        foreach ($colours as $placeToColour) { // this is an array( place, colour)
            $colourLookup[current($placeToColour)] = next($placeToColour);
        }

        // var_dump($colourLookup);
    }

    // now return the approprate colour...
    if (isset($colourLookup[$placeName])) {
        return $colourLookup[$placeName];
    }
    return '';
}