I have the following array:
array (
'nurse' =>
array (
'skill' => ' gfgfgfdNurse Practitioner',
),
'Nurse Practitioner' =>
array (
'skill' => ' gfgfgfdNurse Practitioner',
),
)
I am trying to change the index name so that it is the same as the skill. I am using a foreach
loop to change the skill, but have been unable to figure out how to get the same change to index for the array.
Here is my code:
foreach ($job_titles as $jd => $index){
$job_titles[$jd]['skill'] = "gfgfgfdNurse Practitioner";
}
How do I change the index name?
you can try this
foreach ($job_titles as $jd => $index){
$job_titles[$jd]['newKeyName'] = $job_titles[$jd]['skill'];
unset($job_titles[$jd]['skill']);
}
Providing examples of the output would be great.
Any way, here are two different solutions producing two different results:
$arr1 = array_map(function($v){ return $v['skill']; }, $job_titles);
$arr2 = array_map('array_flip', $job_titles);
You cannot change the index name but instead, you can add required index and remove the skill index. Try this code, Hope it helps.
foreach ($job_titles as $jd => $index){
$job_titles[$jd]['gfgfgfdNurse Practitioner'] = 'gfgfgfdNurse Practitioner';
unset($job_titles[$jd]['skill']);
}
You can use array_walk
with unset
array_walk($a, function(&$v, $k){
$v['new'] = &$v['skill']; //Assign existing values to new index
unset($v['skill']); // remove the existing index
});
Working example : https://3v4l.org/vf36W