0

I have two types of arrays:

1:
$array1["a"][] = "value1";
$array1["a"][] = "value2";
$array1["b"][] = "value3";

2:
$array2["0"] = "a";

What I need now is to somehow find difference between these two arrays. I need to filter out array1 by key, which is located in array2 value. I have tried doing the following:

array_diff(array_keys($array1), array_values($array2));

But I get the following error on that line: ErrorException Array to string conversion

Any ideas?

Marius
  • 119
  • 8

2 Answers2

0

Something like this?

foreach ($array1 as $key => $value)
   if( array_search ($key , $array2 ))
    unset($array1[$key]);

If $array1 needs to have the values, you just need to put the diff in $array1 :

$array1 = array_diff(array_keys($array1), array_values($array2));
Nebulosar
  • 1,727
  • 3
  • 20
  • 46
0

Depending on how you constructed your arrays, it should work. The following code (based on your question) worked:

<?php
$array1=array("a" => array(),"a" => array(),"b" => array());
$array2=array("0"=>"a");
print_r(array_keys($array1));
echo("<br/>");
print_r(array_values($array2));
echo("<br/>");
print_r(array_diff(array_keys($array1), array_values($array2)));       
>

This results in:

Array ( [0] => a [1] => b )
Array ( [0] => a )
Array ( [1] => b )
jhenderson2099
  • 956
  • 8
  • 17