简体   繁体   中英

Replace keys and values in array

I have two arrays:

<?php
    $c = array('john' => 34,'mary'=>22,'rita'=>19);
    $d = array('malone' => 43);
?>

How do I go about changing the key mary and merging the value of $d into $c ? My apologies if this sounds like a dumb question but I really don't know how and I have searched on-line to no solution.

What I did to solve this:

$newkey =array('jane'=>1);
unset($c['mary']);
$e = array_merge($c,$newkey);
print_r($e);

Thanks though.

You can use unset() to remove a key from an array and then concatenate the two arrays as follows:

<?php
    $c = array('john' => 34,'mary'=>22,'rita'=>19);
    $d = array('malone' => 43);
    unset($c['mary']);
    $c += $d;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM