6
I have the arrays as below:
$records = [
["id"=>"1", "name"=>"Alpha"],
["id"=>"2", "name"=>"Bravo"],
["id"=>"3", "name"=>"Charlie"],
["id"=>"4", "name"=>"Delta"],
["id"=>"5", "name"=>"Echo"],
];
$codes = [2,4];
I need to print only the names contained in the variable $records
corresponding to each variable code $codes
awaiting the outcome:
Bravo
Delta
So I made the following code:
foreach ($codes as $i) {
foreach ($records as $j) {
if ($i == $j['id']) {
echo "<p> {$j['name']} </p>";
}
}
}
Is there any way to get the same result easier?
I found it bad the way I’m doing because for each variable code $codes
i do a full scan on variable $records
comparing the values.
A "correct" and efficient use of
array_column
+1– Guilherme Nascimento
Perfect, thank you very much!
– Laércio Lopes