I have an array in which there are users values are stored that will be sent to execute(); function for mysqli query. So, I want the index values to be replaced.
Now what I have done here is used str_replace(); function to do so within foreach loop but the problem is that how I will get the updated array?
$fields = array(
'field_id' => '123',
'field_name' => 'test_name'
);
foreach ($fields as $key => $field) {
$val = str_replace($key, $key, ':'.$key);
$data = array();
$data[$val] = $field;
}
//I only got the value for the last index 1st is not there
print_r($data);
//output which I am expecting will be the following
$fields = array(
':field_id' => '123',
':field_name' => 'test_name'
);
Please let me know if any one could help me out
$fields[':'.$key] = $field; unset $fields[$key];str_replace(searchterm, replacement, subject), you're doing it wrong, probably ... also, you should create the empty array before the loop, not inside, or only the last entry will remain ...