Hello,
I want to replace a array element.
I fetch the array from the database and if the search string is exist in that array, replace with my search string and update the array.
for ex:
$arrdata_frmtable = (56,60,1, 10,95,63,09,11)
$search_string = 1;
replace_string = 22;
$replace_arrdata_finish(56,60,22, 10,95,63,09,11);
after replace search string in an array i want to update the array data into table.
Thanks in advance,
thendral
Find and replace within an array.
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Find and replace within an array.
Have a look at array_splice()
array_splice()
array_splice()
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Find and replace within an array.
Hi,
I used the below code. Is there any other way of code available. If yes let me know about it.
array_splice() function is not suitable for this requirement.
code:
Thanks in advance,
thendral.
I used the below code. Is there any other way of code available. If yes let me know about it.
array_splice() function is not suitable for this requirement.
code:
Code: Select all
/*
1. select data from the table
2. seperate data by comma
3. get arrray position
4. replace with replace string and strore another variable and finally update table
*/
$qry_2 = "select * from table_name";
$this->res = $this->query($qry_2);
if( $this->getCount() > 0 ) {
while ( $db_List = $this->fetchonce($this->res) ) {
$arr_value = explode(',', $db_List['selected_list']);
$onshow_uid = $db_List['uid'];
$onshow_adfile_uid = $db_List['adfile_uid'];
$search_string = $search_sitecd;
$arr_position = array_search($search_string ,$arr_value);
$tot_array_count = count($arr_value);
$tot_value ="";
if(in_array($search_string,$arr_value)) {
$replacement = $txtsitecd;
for($i=0;$i<count($arr_value);$i++) {
if($arr_value[$i] == $search_string && $i == $arr_position) {
$arr_value[$i] = $replacement;
}
if($i < $tot_array_count - 1 ){
$final_array = $arr_value[$i] . ",";
} else if ($i == $tot_array_count - 1 ){
$final_array = $arr_value[$i] ;
}
$tot_value = $tot_value . $final_array;
}
$qry_3 = "update query"; //here update query.
}
}
return true;
} else {
return false;
}
thendral.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Find and replace within an array.
Try this:
http://php.net/array-search
Code: Select all
$arrdata_frmtable = array(56,60,1, 10,95,63,09,11);
$search_string = 1;
$replace_string = 22;
$replace_arrdata_finish = $arrdata_frmtable;
$key = array_search($search_string, $replace_arrdata_finish);
if ($key !== false) {
$replace_arrdata_finish[$key] = $replace_string;
}Re: Find and replace within an array.
Hi Jonah Bron,
Thanks a lot. It's a great code.
after change the array data, I need to save into table.
for that i have to use for loop right same as below i gave.
I hope this is way to get array data after replaced string.
Thanks in advance,
thenndral.
Thanks a lot. It's a great code.
after change the array data, I need to save into table.
for that i have to use for loop right same as below i gave.
I hope this is way to get array data after replaced string.
Code: Select all
$arrdata_frmtable = array(56,60,01,1,10,95,63,09,11);
$search_string = 01;
$replace_string = 232;
$replace_arrdata_finish = $arrdata_frmtable;
$key = array_search($search_string, $replace_arrdata_finish);
if ($key !== false) {
$replace_arrdata_finish[$key] = $replace_string;
}
echo($replace_arrdata_finish); // array value.
[b]
for($i=0;$i<count($replace_arrdata_finish);$i++) {
$final_array = $replace_arrdata_finish[$i] ;
echo($final_array); // to save this array into table.[/b]
}
Thanks in advance,
thenndral.