0

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

    }

}
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
hansi wasd
  • 97
  • 1
  • 5

1 Answers1

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']);
  • but my problem is the key values or area or shop are user generated so i dont know them before – hansi wasd Feb 09 '15 at 21:13
  • Show me some code to better explain the problem. You should be able to set it regardless. –  Feb 09 '15 at 21:15
  • $values ["Area"] = "15"; $values ["Shop"] = "430"; $values ["SalesArea"] = "1998"; $values ["info4"] = ""; $values ["info5"] = ""; – hansi wasd Feb 09 '15 at 21:16
  • all there keys area ,shop and salesarea are from the user – hansi wasd Feb 09 '15 at 21:17
  • I'm not understanding what prevents you from doing the above. –  Feb 09 '15 at 21:19
  • and all i want is to replace the user keys with my keys so transform area into info1 or salesarea into info3 – hansi wasd Feb 09 '15 at 21:19
  • im still getting error messages iand i dont know why like 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 09 '15 at 21:21
  • In your array you're referencing the index/key of 10 but it doesn't exist. Make it exist or don't reference it. –  Feb 09 '15 at 21:37