1

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);
EnglishAdam
  • 1,380
  • 1
  • 19
  • 42

7 Answers7

5

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' );
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
  • I was thinking of why you just send as comment instead answer :) – Yusuf K. Nov 28 '11 at 15:45
  • 2
    It was a simple answer, fit in a comment. I think that is better a more robust answer (exemplified), I did it now, but other user can do for me, in future, because of my comment. :) – David Rodrigues Nov 28 '11 at 15:48
  • @David, right on both counts and you're second answer is sooo elegant for a newbie like me that i'll have to write 10 lines of comment to be able to understand it in the future!!! – EnglishAdam Nov 28 '11 at 15:58
  • 1
    I have commented for you to understand better how it works. ;) – David Rodrigues Nov 28 '11 at 16:03
1
$common_keys = array_intersect($ArrayA, array_keys($ArrayB));
$arrayC = array();
foreach($common_keys as $key) {
   $arrayC[$key] = $ArrayB[$key];
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

You only need one loop:

      foreach ($ArrayA as $valueA) {
         $ArrayC [$valueA] = $arrayB[$valueA];
      }
jeroen
  • 91,079
  • 21
  • 114
  • 132
1
$arrayC=Array()
foreach($arrayA as $valueA){
   $arrayC[$valueA]=$arrayB[$valueA];
}
bigblind
  • 12,539
  • 14
  • 68
  • 123
0

You're mixing lowercase/uppercase variable names. You'll want to use $valueB instead of $ValueB.

Also, in your if you are assigning, not comparing.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
0

If you want to do it without looping through the arrays, you could try using a combination of array_intersect_key() and array_flip()

$ArrayA = array(3, 7, 8);
$ArrayB = array(1 => 'bread', 2 => 'banana', 3 => 'fruit',
            4 => 'milk', 5 => 'pizza', 6 => 'gum', 7 => 'corn',
            8 => 'lager');
$searchArray = array_flip( $ArrayA );
$results = array_intersect_key( $ArrayB, $searchArray );

var_dump( $results );
liquorvicar
  • 6,081
  • 1
  • 16
  • 21
-1

Here is a better way to do it:

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

This will give you:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)
Meisam Mulla
  • 1,845
  • 3
  • 23
  • 37