-1

I have this following array:

$arr = [ 'demo_key1' => 'demoval1', 'demo_key2' => 'demoval2' ];

and, i need get this transformed array:

$arr = [ 'demo-key1' => 'demoval1', 'demo-key2' => 'demoval2' ];

So, i want replace _ to - figures, how can i do ?

I tried some examples on Stack Overflow about this for i could not do.

I really thank you very much for your helps.

Jeremy
  • 88
  • 7
  • 1
    Possible duplicate of [How to rename array keys in PHP?](https://stackoverflow.com/questions/9605143/how-to-rename-array-keys-in-php) OR [PHP rename array keys in multidimensional array](https://stackoverflow.com/questions/2212948/php-rename-array-keys-in-multidimensional-array) OR [...](https://stackoverflow.com/search?q=%5Bphp%5D+rename+array+key) – Sean Nov 03 '17 at 23:03
  • @Sean nothing works. – Jeremy Nov 03 '17 at 23:16
  • 1
    show what you have tried. we are not here to do the work for you, but to help guide you. – Sean Nov 03 '17 at 23:19

2 Answers2

0

Here is an example which may help you:

$arr=["demo0"=>"100","demo1"=>["demo_key1"=>"demoval1","demo_key2"=>"demoval2"],"demo3"=> "false"];

function changeKeys($array){
  $newArray=[];
  foreach($array as $key=>$val){
    $newKey=str_replace('_','-',$key);
    if(is_array($val)){
      $newArray[$newKey]=changeKeys($val);
    }else{
      $newArray[$newKey]=$val;
    }
  }
  return $newArray;
}
$arr=changeKeys($arr);

print_r($arr);

Output:

Array
(
    [demo0] => 100
    [demo1] => Array
        (
            [demo-key1] => demoval1
            [demo-key2] => demoval2
        )

    [demo3] => false
)
ferhado
  • 2,363
  • 2
  • 12
  • 35
  • This is working but, i can't use function for I will edit in an existing array. – Jeremy Nov 03 '17 at 23:27
  • `[demo0] => 100 [demo1] => Array ( [demo_key1] => demoval1 [demo_key2] => demoval2 ) [demo3] => false` I need to directly update it in the main array. – Jeremy Nov 03 '17 at 23:33
  • thanks for your helps but, i can not update all array (use function) i need directly transformation, like this `foreach ($arr as &$value) { if ( $value == 1 ) { $value = 2; } }` I have to do it in the same place. – Jeremy Nov 03 '17 at 23:50
  • This function will update all sub arrays see the output, when this is not waht you want please give more details waht you want – ferhado Nov 03 '17 at 23:52
  • Thanks so much for your interest, I solved it via a different way. – Jeremy Nov 04 '17 at 00:09
  • Nevertheless try it – ferhado Nov 04 '17 at 00:12
0

You can use array_keys, str_replace, array_values and array_combine in one expression:

$arr = array_combine(str_replace('_', '-', array_keys($arr)), array_values($arr));

If you need in-place replacement, maybe because you have a reference to the array, then you can do it like this:

function cleanArrayKeys(&$arr) { // use a reference
    $arr = array_combine(str_replace('_', '-', array_keys($arr)), array_values($arr));
}

$arr = [ 'demo_key1' => 'demoval1', 'demo_key2' => 'demoval2' ];
cleanArrayKeys($arr); 

print_r($arr); // same array reference now has the updated keys
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you for your response, `[demo0] => 100 [demo1] => Array ( [demo_key1] => demoval1 [demo_key2] => demoval2 ) [demo3] => false` I need to directly update it in the main array. What do you suggest in this regard? – Jeremy Nov 03 '17 at 23:34
  • keys cannot be updated, only replaced. And when you replace keys (i.e. delete + recreate), you might as well reassign the whole array. But you have already accepted an answer, so I suppose you are OK. – trincot Nov 03 '17 at 23:35
  • Can not I update it like this? `foreach ($arr as &$value) { if ( $value == 1 ) { $value = 2; } }` – Jeremy Nov 03 '17 at 23:40
  • You're not updating keys there, only values. But hey, you already accepted an answer, so why are you still asking? – trincot Nov 03 '17 at 23:40
  • The answer I approved exactly was not working for my problem. Actually, i can not update all array (use function) i need directly transformation, like this `foreach ($arr as &$value) { if ( $value == 1 ) { $value = 2; } } ` I have to do it in the same place. – Jeremy Nov 03 '17 at 23:53
  • Like I said that code is replacing *values*, not *keys*. I don't understand. – trincot Nov 03 '17 at 23:57
  • I added code to my answer to show how it works also with call-by-reference. – trincot Nov 04 '17 at 00:03
  • Yes this code is replacing values, i want to change the keys as the same building (directly or in foreach). – Jeremy Nov 04 '17 at 00:04
  • Like I said, keys cannot be updated like you can update values. You can only delete them, and recreate others. See my extended answer. – trincot Nov 04 '17 at 00:05
  • Thanks so much for your interest, I solved it via a different way using your code. – Jeremy Nov 04 '17 at 00:11