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

  • SEARCH
  • Home
  • 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 6875409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:19:10+00:00 2026-05-27T04:19:10+00:00

I have two arrays. ArrayA is numeric (ArrayA => 3, 7, 8) ArrayB is

  • 0

I have two arrays.

ArrayA is numeric

(ArrayA => 3, 7, 8)

ArrayB is an associative array

(1=>bread, 2=>banana, 3=>fruit, 4=> milk, 5=>pizza, 6=> gum, 7=>corn, 8=>lager)

I want to create a third array, ArrayC, associative, that matches the Values of ArrayA with ArrayB keys, and gives ArrayA keys the relevant ArrayB values.

In other words, from this example

ArrayC (3 => fruit, 7=>corn, 8=>lager)

However my code is only giving me

ArrayC (3 => lager, 7=>lager, 8=>lager) //last value of ArrayB

Code:

     $ArrayC = array();
     foreach ($ArrayB as $keyB => $valueB) {
          foreach ($ArrayA as $valueA) {
             if ( $valueA = $keyB) {
                   $ArrayC [$valueA] = $valueB;
             }
          }
      }
      print_r($ArrayC);
  • 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-05-27T04:19:11+00:00Added an answer on May 27, 2026 at 4:19 am

    First: if need compare with ==, not with = (it set value).

    Second: this is a better version of code:

    // The keys that you want copy from ArrayB
    $ArrayA = array(3, 7, 8);
    // The values that will be copied
    $ArrayB = array(1 => 'bread', 2 => 'banana', 3 => 'fruit',
                    4 => 'milk', 5 => 'pizza', 6 => 'gum', 7 => 'corn',
                    8 => 'lager');
    // The filtered array (based on ArrayA and ArrayB)
    $ArrayC = array();
    
    // For each ArrayA values (3, 7 and 8), trigger this routine
    // setting ArrayKey as each value of ArrayA, one for time
    foreach($ArrayA as $ArrayKey) {
        // Basically: $ArrayC [3] = $ArrayB [3]; (...)
        $ArrayC [$ArrayKey] = $ArrayB [$ArrayKey];
    } 
    
    // Final result will be:
    // $ArrayC = array( 3 => 'fruit', 7 => 'corn', 8 => 'lager' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boolean array call it flag . I have two numeric arrays
I have two arrays, both associative; ArrayUneaten ( lemon=> 7, banana=>6, apple=>10)//the units of
Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285};
I have two arrays: Array ( [2005] => 0 [2006] => 0 [2007] =>
Consider I have two arrays: $friends = Array('foo', 'bar', 'alpha'); $attendees = Array('foo', 'bar');
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays in php $arrNum = array(1.7, 1.52, 0.01, 0.11); $arrStr =
im trying to merge two arrays together. both have numeric keys and are unique.
I have two arrays: Array ( [0] => Array ( [id] => 1 [type]

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.