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

Can i change my key with my own words in php. Please suggest

//Expecting like this

Array
(
    [h] => 3
    [c] => 5
    [s] => 7
    [e] => 8
    [r] => 4
    [t] => 6
)
2
  • Did did you create the array? Commented Aug 19, 2016 at 11:41
  • array_combine(['h','c','s','e','r','t'],[3,5,7,8,4,6]) Commented Aug 19, 2016 at 11:42

3 Answers 3

1

Sure you can. Your syntaxes are wrong. you can use arrays like this.

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];

For more clarification check the official php Maual

Sign up to request clarification or add additional context in comments.

Comments

0

You can also try this

$myarr = array(3,5,7,8,4,6);
$newkeys = array('h','c','s','e','r','t');
$c=array_combine($newkeys ,$myarr);

Comments

0

You could use an assign and an unset this way

$your_array[$your_newkey] = $your_array[$your_oldkey];
unset($your_array[$your_oldkey]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.