Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

hello i have an array like this and i want to change the area key to info1 like $values ["Area"] will become $values ["info1"],$values ["Shop"] should be $values ["info2"] and so on but evertime I run my code it throws errormessages like "undefined index" but I really dont know why.

$values ["standort"] = "60071-00001";
$values ["desc"] = "car";
$values ["street"] = "first ave";
$values ["number"] = "98";
$values ["postcode"] = "40764";
$values ["city"] = "London";
$values ["Area"] = "15";
$values ["Shop"] = "430";
$values ["SalesArea"] = "1998";
$values ["info4"] = "";
$values ["info5"] = "";

// echo var_dump(array_keys($values));

for($i=6;$i<=10;$i++){
    $j= array_keys($values)[$i];
    if($i==6){
        $values["info1"]=$values[$j];
        unset($values[$j]);

    }
    if($i==7){
        $values["info2"]=$values[$j];
        unset($values[$j]);

    }
    if($i==8){
        $values["info3"]=$values[$j];
        unset($values[$j]);

    }
    if($i==9){
        $values["info4"]=$values[$j];
        unset($values[$j]);

    }
    if($i==10){
        $values["info5"]=$values[$j];
        unset($values[$j]);

    }

}
share|improve this question

marked as duplicate by Marc B Feb 9 at 21:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Plz show the exact error message(s). –  mopo922 Feb 9 at 21:07
    
Notice: Undefined offset: 10 in C:\xampp\htdocs\php\php\index.php on line 17 Notice: Undefined index: in C:\xampp\htdocs\php\php\index.php on line 39 –  hansi wasd Feb 9 at 21:12

1 Answer 1

There is no true way to "replace" a key in an array in PHP, but you can add a new one and then remove the old one like so:

$values['info1'] = $values['Area'];
unset($values['Area']);
share|improve this answer
    
but my problem is the key values or area or shop are user generated so i dont know them before –  hansi wasd Feb 9 at 21:13
    
Show me some code to better explain the problem. You should be able to set it regardless. –  Noah Matisoff Feb 9 at 21:15
    
$values ["Area"] = "15"; $values ["Shop"] = "430"; $values ["SalesArea"] = "1998"; $values ["info4"] = ""; $values ["info5"] = ""; –  hansi wasd Feb 9 at 21:16
    
all there keys area ,shop and salesarea are from the user –  hansi wasd Feb 9 at 21:17
    
I'm not understanding what prevents you from doing the above. –  Noah Matisoff Feb 9 at 21:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.