How to rename elements of an array in php

1

I want to rename the values of an array in php, change this array:

$value('valor1','valor2');

to this other:

$value('v1','v2');
    
asked by matteo 29.03.2016 в 21:58
source

1 answer

2

You can use array_replace to replace item values within the array:

<?php
  $value = array('valor1', 'valor2');
  //aquí reemplazas los valores del array!
  $replacements = array(0 => 'v1', 1 => 'v2');
  $final_array = array_replace($value, $replacements);
  print_r($final_array);
?>

According to the previous script, we would initially:

Array
(
    [0] => valor1
    [1] => valor2
)

and replacing the values within the array with array_replace as final values:

Array
(
    [0] => v1
    [1] => v2
)

You can see the example here .

    
answered by 29.03.2016 в 23:02