Simplify value localization in arrays in PHP

Asked

Viewed 99 times

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.

4 answers

6


whereas the value of id is unique for each name, you can define a map that relates the id to the respective name. In PHP, we can define this map through a array associative in which the key is id and the value is its name. We can mount this map with a line of code:

$nomes = array_column($records, "name", "id");

This will generate the array:

Array
(
    [1] => Alpha
    [2] => Bravo
    [3] => Charlie
    [4] => Delta
    [5] => Echo
)

Thus, it would be enough to access the positions $nomes[2] and $nomes[4], or:

foreach ($codes as $code) {
    echo $nomes[$code], PHP_EOL;
}

Displaying the desired names.

5

Could use array_filter to filter information and in_array to search if there is a similar code, example:

<?php

$records = [
    ["id"=>"1", "name"=>"Alpha"],
    ["id"=>"2", "name"=>"Bravo"],
    ["id"=>"3", "name"=>"Charlie"],
    ["id"=>"4", "name"=>"Delta"],
    ["id"=>"5", "name"=>"Echo"],
];

$codes = [2,4];


$array_result = array_filter($records, function($item) use ($codes) {
    return in_array($item['id'], $codes);
});


foreach($array_result as $value){
    echo $value['name'];
}

IDEONE ONLINE EXAMPLE

  • Solucionado @Guilhermenascimento !!!

  • Virgilio Novic show!

  • 1

    Thanks friend! helped a lot! + 1

4

Use array_filter+foreach and use array_search+array_column+foreach is no better than what you have already asked in your question, which are only 2 foreachs+if, not only of performance, but even of readability of the code, that in the code of the question this best. The use of array_search, array_filter and other functions will not always be better than making 1 or 2 loops, sometimes it will be the same thing, in the case of the Anderson example, the use of array_collunm was the most efficient speaking of the specific function

Still not necessarily the most efficient way in general.

Most often the simplest is the best, and in the case of the need of the author of the question a in_array and a single foreach would already solve everything and keep the code still more readable:

$records = [
    ["id"=>"1", "name"=>"Alpha"],
    ["id"=>"2", "name"=>"Bravo"],
    ["id"=>"3", "name"=>"Charlie"],
    ["id"=>"4", "name"=>"Delta"],
    ["id"=>"5", "name"=>"Echo"],
];

$codes = [2,4];

foreach ($records as $j) {
    if (in_array($j['id'], $codes)) {
        echo "<p> {$j['name']} </p>";
    }
}

Example: https://ideone.com/UFXWqO

  • 1

    How are you sure of that? array_filter+foreach e usar array_search+array_column+foreach !!! and something else if the codes are not the same array_column is not efficient is a mistake, so it is better to produce a code that will fit in all spheres than to find it by an example of the question ...

  • 1

    Caro @Virgilionovic array_column was efficient in the example cited only, Anderson, if the codes are different then even the question of the author does not make sense, agree? Now yes, array_filter+foreach is almost the same thing 2 foreachs, don’t get me wrong, but it’s still 2 "loops" ... of course internally in the first case, but anyway neither need 2 loops, regardless of the way you use, the answer here indicates just this ... a loop only already solves.

  • 1

    we analyze the code ( was efficient in the example cited only, Anderson, if the codes are different then even the question of the author does not make sense, agree ) beyond what question this is a premise here ... !!! then the array_column only efficient so if the Id is different from the order the code will break ...!!!!

  • I can agree with one but the rest is something we can question

  • @Virgilionovic what else? The only part I’m talking about using column array_is a sentence, the rest is about not necessarily the examples provided being better ... incidentally, I didn’t say columnnm array_is the best, I said that the specific use of the form made was the efficient way to use the function and not that it is the most efficient way to solve the author’s problem ... all answers used more things ... and the answer here indicates that it is best to use less. There’s a lot of things that people complicate by thinking they’re making it easier, I hope you understand.

  • I understood perfectly well what they talked about, but if applying to my situation the array_column fits perfectly. Thank you very much. + 1

  • 2

    @Laérciolopes yes, the way that Anderson presented I even imagined that it would fit better for you, the criticism here of the whole debate was the indiscriminate use of loops and the loss of readability of the other proposed solutions, one thing you have to keep in mind, if it is possible to simplify then there really is no reason to complicate something ... many people in the world of Design Patterns suffer from the false feeling that "complicate" is better.

Show 2 more comments

3

Can use array_search (array search) and array_column (search for a specific column):

foreach ($codes as $i) {
   echo "<p> {$records[array_search($i, array_column($records, 'id'))]['name']} </p>";
}

It will fetch in the column id the values of the array $codes and return the key value name.

The array_search($i, array_column($records, 'id')) will return the index of the item in the array $records where the key id matches the array values $codes. It would be like:

> $records[1]['name']
> $records[3]['name']

Check on IDEONE

It will only work in PHP version 5.5 onwards because of array_column.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.