简体   繁体   中英

php match keys and copy values to new array

I have two arrays. One contains a list of places.

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

The other contains a long list of places and a colour for each

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

I'm trying to build a final array in which for every place in $places, I have a corresponding colour, taken from the $colours list.

The closest I have gotten is

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

But it's not producing the colour of each location from $colours. Can anyone point me in the right direction?

This should do it:

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

try this

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

As usual, late to the party... :-) Someone may find it useful.

Here is a tested version (PHP 5.3.18), that uses the original data structures. It should be faster for large input arrays because it converts the 'list of 'place', colour' into an array with a key of 'place' that has a value of 'colour'. ie you can look up the colour for a 'place' directly. The reqired 'colour' can then be 'looked up' directly given the 'place'.

The code is commented.

<?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 '';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM