Thank you in advance for your answers and comments.
I want to achieve the following I have two arrays with key and value, the first is an array with several values and the second an array of arrays with several values, what I am trying to achieve is to keep the keys and value of my first array whose keys match the key "value" of my second array. Enter code to make me understand better:
$array1 = [
"id" => 13,
"name" => "MAURO",
"code" => "2903",
"lastname" => "GARCIA",
"email" => "[email protected]",
"second_lastname" => "VARELA",
"rfc" => "GAVM540516",
"curp" => "GAVM540516HJCRRR02",
"nss" => "70707070707",
];
$array2 =
[
[
"id" => 14,
"name" => "rfc",
"isActive" => true,
"value" => "rfc",
],
[
"id" => 15,
"name" => "numero de empleado",
"isActive" => true,
"value" => "code",
],
];
From array1 I want to keep the values whose key matches the value of the key value of my second array, that is, a result like this:
$resultado = [
"rfc" => "GAVM540516",
"code" => "2903"
];
I had achieved something similar to this in the scenario that my array2 is a simple array I share code:
$array1 = [
"id" => 13,
"name" => "MAURO",
"code" => "2903",
"lastname" => "GARCIA",
"email" => "[email protected]",
"second_lastname" => "VARELA",
"rfc" => "GAVM540516",
"curp" => "GAVM540516HJCRRR02",
"nss" => "70707070707",
];
$array2= ["rfc","code"];
var_dump(array_intersect_key($array1, array_flip($array2)));
This works the problem I have now is that my dos array is now an array of arrays. Thanks again for your answers.
You can achieve what you want with the function
array_key_exists()
like this:Hello, I share how I solved my problem, although I think there will be another cleaner way to solve it, I'm still open to your answers.
I share my solution: