Assigning values to a numeric array of values a PHP query

Asked

Viewed 48 times

1

Given the table marcas of the database teste_db (for example)

| id |  marca  |

| 1  | mercedes|
| 2  |   audi  |
| 3  |   fiat  |
| 4  |   alfa  |
| ...|   ...   |
|573 | ferrari |
|574 | bentley |

and given an array with n numerical arguments such as:

$arr = array('3','23','127','574')

and assuming that the array will always contain numerical arguments that can be found in the column id of this table, I would like to include in the array, for each argument of this, the value marca whose id table has the same array value, and the result would be:

$arr = array("3"=>"fiat", "23"=>"rover", "127"=>"tata", "574"=>"bentley");

What code would be required for this task? PHP and mysqli.

  • And what exactly would it be $k and $v? You can [Edit] the question and add how the desired result would look?

  • The $kand the $v are the keys and values, respectively of an array where array ( $k => $v)

  • Of which array? You’re confused. What is the relationship of array $arr and the database table? What exactly do you want to do? Remember to add to the question the example of the expected result.

  • If the procedure is done, as I imagine, using mysqli_query, is obviously implicit the connection to the database. My question will be after this step.

1 answer

1

If I understand correctly, you can do something like:

if ($result = mysqli_query('SELECT marca FROM marcas'))
{
    $arr = array('1', '2', '3', '4');
    $bancoDeDados = mysqli_fetch_all($result);
    $resultado = array_map(null, $arr, $bancoDeDados);

    print_r($resultado);
}

See working on Ideone | Repl.it

That the result would be something similar to:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => mercedes
        )

    [1] => Array
        (
            [0] => 2
            [1] => audi
        )

    [2] => Array
        (
            [0] => 3
            [1] => fiat
        )

    [3] => Array
        (
            [0] => 4
            [1] => alfa
        )

)

With the function array_map, passing the first parameter as null, will be returned a pair with the respective values of $arr and $bancoDeDados.

  • The intended result is this but I want to match the array values with those of the table that may not be ordered or followed.

  • @Goncalorsr now understands why I asked the relationship of array with the database? This was not clear in the question.

  • @Goncalorsr edited the ersposta. See if this is it, because if it is not, there are more things that are not clear in the question.

  • Perhaps I could have specified more... my fault. The table I gave as an example is actually a table with 573 entries. Coming from another part of the code, which is not relevant, I get an array that can have 1 or 2 or 3 or 30 or 50 values between 1 and 573 (eg: array(2, 5, 24, 36, 345)). Now I wanted to associate to each array value the "mark" value whose id in the table has the same array value, and result in array('2' =>'Nissan', '5' =>'Ferrari', '24' => 'Bentley' ... and so on.

  • @Goncalorsr then the question is far from asking that. Please edit the question and detail what you commented. Only after that I will edit the answer with the solution.

Browser other questions tagged

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