0

I dont want to merge or intersect or diff I just want to get both values from both arrays with matching keys regardless of values matching or not.

example data

array1 ( 0 => 'a', 1 => 'b' )
array2 ( 0 => 'a', 1 => 'c' )

foreach

echo "Key: ".$key." Value1: ".$v1." Value2: ".$v2."";

I would like this as the output

Key: 0 Value1: a Value2: a
Key: 1 Value1: b Value2: c

2 Answers 2

2

If both arrays have the exact same keys, you can iterate through one of them while printing values from both.

foreach ($array1 as $key => $val) {
   echo "Key: ".$key." Value1: ".$array1[$key]." Value2: ".$array2[$key];
}
Sign up to request clarification or add additional context in comments.

1 Comment

OMG THIS WORKED THANKS SO MUCH <3
0
$array1= array( 0 => 'a', 1 => 'b' );
$array2=array ( 0 => 'z', 1 => 'c' );
foreach ($array1 as $k=>$val){
   //safe in case of key does't exist in second array
   if(array_key_exists($k,$array2))
     echo $k . " Value1: ".$val ." Value2: ". $array2[$k].'<br>';
   else
     echo $k . " Value1: ".$val
}

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.