Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:41:23+00:00 2026-06-17T22:41:23+00:00

I have two associative arrays, one with data about states (or provinces) and one

  • 0

I have two associative arrays, one with data about states (or provinces) and one with different countries. I’m getting the values from a relational database so the states array doesn’t hold the country name but rather the country id.

States Array:

array(3) {
  array {
    'name' => 'Alabama',
    'country' => 1},
  array {
    'name' => 'Alberta',
    'country' => 2},
  array {
    'name' => 'Minnesota',
    'country' => 1}
}

Countries Array:

array(2) {
    1 => 'United States',
    2 => 'Canada'
}

The result I want is:

array(3) {
  array {
    'name' => 'Alabama',
    'country' => 'United States'},
  array {
    'name' => 'Alberta',
    'country' => 'Canada'},
  array {
    'name' => 'Minnesota',
    'country' => 'United States'}
}

I searched and searched and haven’t found a question that answers this specific problem. I’m sure I’m not the only one who has data like this; maybe I’m not looking for the right terms. I did write a function in my Country model class to solve this but I’m thinking there has to be a more efficient way.


My current solution (it works but there’s probably a better way, right?):

function replaceKeyValues($replaceArray,$replaceKey)
  {
  $countries = $this->get_countries();
  foreach ($replaceArray as &$itm) {
    if (isset($itm[$replaceKey])) {
      if (in_array($itm[$replaceKey], array_flip($countries))) {
        $itm[$replaceKey] = $countries[$itm[$replaceKey]];
      }
    }
  }
  return $replaceArray;
}

//called like this (for example)
$states = $this->Country->replaceKeyValues($states,'country');

Edit: I ended up using Jon’s suggestion and also slightly modified the original function to make it generic enough that I don’t need it in my model class anymore. It’s now in one of my helper classes, and I added another input parameter which is the “master data” array. Hopefully this helps someone 🙂

function replaceKeyValues($masterArray,$replaceArray,$replaceKey)
{
    foreach ($replaceArray as &$itm) {
        if (isset($itm[$replaceKey])) {
            if (isset($masterArray[$itm[$replaceKey]])) {
                $itm[$replaceKey] = $masterArray[$itm[$replaceKey]];
            }
        }
    }
    return $replaceArray;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:41:24+00:00Added an answer on June 17, 2026 at 10:41 pm

    Apart from the fact that this

    if (in_array($itm[$replaceKey], array_flip($countries)))
    

    would be much better written as

    if (isset($countries[$itm[$replaceKey]]))
    

    and the fact that the nested conditions can be de-nested, this is basic enough that there’s no real opportunity for improvement.

    You could of course replace the foreach with array_walk and a lambda, but that wouldn’t be any “better”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sequential (non-associative) arrays whose values I want to combine into a
If I have two associative arrays, what would be the most efficient way of
I have two associative arrays in PHP that are defined as following: $this_week[] =
I have two arrays, one that is made up of NSDates and one that's
I have two associative arrays that can look as follows: $prod1 = ( [name]
using Crystal XI I have two arrays - one captures a list of timepoints
I have two associative arrays. I need to subtract ($price - $tax) to get
I have two arrays, one is a node-node-cost array [a_node,b_node,cost] which has 8000 entries
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have an associative array with two object inside. Running this through $(myassoc).each() ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.