Page 1 of 1

Find and replace within an array.

Posted: Mon Jan 17, 2011 5:17 am
by thenndral
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

Re: Find and replace within an array.

Posted: Mon Jan 17, 2011 10:50 am
by social_experiment
Have a look at array_splice()
array_splice()

Re: Find and replace within an array.

Posted: Tue Jan 18, 2011 9:41 pm
by thenndral
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:

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;	
	}

Thanks in advance,
thendral.

Re: Find and replace within an array.

Posted: Tue Jan 18, 2011 10:37 pm
by Jonah Bron
Try this:

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;
}
http://php.net/array-search

Re: Find and replace within an array.

Posted: Thu Jan 20, 2011 7:27 pm
by thenndral
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.

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.