-1

How can I replace my array key to particular name

Array ( [0] => 0 [1] => 1 [2] => 3 [3] => 4 [4] => 5 )

  • Try looking at the link below hopelly its helpful http://stackoverflow.com/questions/2917513/replace-string-key-of-php-array – Programmer man Oct 08 '14 at 13:50
  • What key do you want to change, and what is the logic behind why and how it will change? – Mike Brant Oct 08 '14 at 13:55
  • @MikeBrant Brant i am using json_encode to encode array and output is not what i wants suppose my result is 'code' {"0":["Monday","5:30 PM"],"1":["Friday","6:00 PM"],"3":["Tuesday","4:15 PM"],"4":["Wednesday","8:30 PM"],"5":["Thursday","1:45 PM"] 'code' and this is what i wants 'code' [{"label":"Monday","5:30 PM"},"label":{"Friday","6:00 PM"},"label":{"Tuesday","4:15 PM"},"label":{"Wednesday","8:30 PM"},"label":{"Thursday","1:45 PM"}] 'code' – Rachit Gupta Oct 08 '14 at 13:58
  • @RachitGupta Add the additional information in your comment to your question. It is unreadable as is. – Mike Brant Oct 08 '14 at 14:05

1 Answers1

0

If you're looking to replace a key based on the value, you could use this code: (assuming you want to replace Array[4])

<?php

foreach ($array as $key => $value) {
    if ($value == "5") {
        $array['new_key'] = $value;
        unset($array[$key]));
    }
}

If you want to define a specific key, you only have to say so:

<?php

$array['new_key'] = 'some_value';
helllomatt
  • 1,331
  • 8
  • 16