Jammy
Jammy

Reputation: 11

array change key name with own words

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
)

Upvotes: 0

Views: 47

Answers (3)

SG_
SG_

Reputation: 496

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

Upvotes: 1

Passionate Coder
Passionate Coder

Reputation: 7294

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);

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

You could use an assign and an unset this way

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

Upvotes: 0

Related Questions