fill an array with Indice

Asked

Viewed 45 times

-2

I own the following arrays:

$lista_geral= array(
    0|7897748704559 =>'',
    0|7897748704009 =>'',
    0|7897748705501 =>'', 
    0|7897748704160 =>'', 
    0|7897748704573 =>'', 
    0|7897748704238 =>'', 
    0|7897748705587 =>'', 
    0|7897748705556 =>'', 
    0|7897748704337 =>'',
    0|7897748704320 =>'', 
);
$lista_quantidade = array(150,10,6,12,45,50,5,25,3,2);

I want to take the values of array($lista_geral) and put in order on array($lista_quantidade) thus:

Array
(
    [0|7897748704559] => 150
    [0|7897748704009] => 10
    [0|7897748705501] => 6
    [0|7897748704160] => 12
    [0|7897748704573] => 45
    [0|7897748704238] => 50
    [0|7897748705587] => 5
    [0|7897748705556] => 25
    [0|7897748704337] => 3
    [0|7897748704320] => 2
)

I’ve tried using the array_push but it didn’t work, I would like some help please.

My code:

foreach($lista_quantidade as $ListaQuantidade){
    array_push($lista_geral, $ListaQuantidade);
}
  • The 0| of 0|7897748704160 must disappear?

  • he is irrelevant can remain

  • if you have access to the indexes and know that they always start with 0|alguma_coisa, you can change the content to remove the 0|.

1 answer

0


You can create a new array and insert the key of the first one with the value of the second:

$lista_geral= array(
    0|7897748704559 =>'',
    0|7897748704009 =>'',
    0|7897748705501 =>'', 
    0|7897748704160 =>'', 
    0|7897748704573 =>'', 
    0|7897748704238 =>'', 
    0|7897748705587 =>'', 
    0|7897748705556 =>'', 
    0|7897748704337 =>'',
    0|7897748704320 =>'', 
);
$lista_quantidade = array(150,10,6,12,45,50,5,25,3,2);

$lista_nova = array();
$i = 0;

foreach($lista_geral as $chave => $valor){
    $lista_nova[$chave] = $lista_quantidade[$i];
    $i++;
}

Browser other questions tagged

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