1

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?

4
  • 1
    foreach($places as $key => $value) should point you in the right direction. Commented Nov 1, 2014 at 13:00
  • Can you post an array of your exact desired output? Commented Nov 1, 2014 at 13:02
  • change the 'places' to 'colors' array to be of the form: array('place' => 'colour', ...). Then the 'place' is the 'key' to lookup the corresponding colour. Currently you will have to scan the 'places - colours' array to find the 'places' entry. Commented Nov 1, 2014 at 13:08
  • Ideally: $result = Array ( [0] => Array ( ['place'] => London ['colour'] => Yellow) ) Commented Nov 1, 2014 at 13:09

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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'. i.e. 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 '';
}

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.