Join two array in php

Asked

Viewed 44 times

0

Good morning guys, a quick question. I need to merge two array of a queries in the following way:

$tamanhos = array('p', 'm', 'g');
$valor_item = array('10', '20', '30');

where "p" has to be equal to 10, m = 20, g = 30

This can make a foreach that joins the two arrays to display the size with the value of each item.

I think this way I could understand better what I need.

Thank you and in the waiting.

  • 1

    Puts an example of how you want it to look, it’s unclear. This example of yours doesn’t even have arrays (if you take syntax seriously).

1 answer

1


First of all review the syntax of arrays.

$tamanhos = array('p', 'm', 'g');
$valor = array('10', '20', '30');
$result = array_combine($tamanhos, $valor);

print_r($result);

running on the ideone

Upshot:

Array
(
    [p] => 10
    [m] => 20
    [g] => 30
)

torray_combine - Creates an array using an array for keys and another for values

  • @Saul, if the answer is right mark as accepted or an example of how the result would be in your question

Browser other questions tagged

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