Jump to content

Decoding ASCII value encoded characters into UTF-8 characters inside an array?


chainmatrix

Recommended Posts

Situation is as follows: I have fetched data from database. This data has all special characters replaced with their ASCII value codes (ä is ä , ö is ö and so on). Due to the encoding, data is not in correct alphabetical order after fetching it; thus I'd need to decode all the characters back into normal UTF-8 characters and then sort the data. html_entity_decode seems to handle this. However, I am unable to figure out how to decode the data, as it is currently located in a multidimensional array?

 

I started solving this by going through the arrays with loops; my current code is below, but my knowledge in arrays in not good enough to figure out how to replace the data within the array with the decoded values?

 

foreach ($result_array as $row) {
foreach ($row as $key => $value) {			
	$new_value = html_entity_decode($value);
	// I would now need to replace the original $value in $result_array with the $new_value, but can't figure out how to do it??
}
}

Just change the variable name to $value.  It will overwrite the current value in the loop.

 

Thank you for the answer. However, that does not seem to update the original values in the array; after running through the loop, they remain unchanged?

 

assign $value as a reference in your foreach and it should work

foreach ($result_array as $row) {
foreach ($row as $key => &$value) {			
	$value = html_entity_decode($value);
	// I would now need to replace the original $value in $result_array with the $new_value, but can't figure out how to do it??
}
}

Notice how I put an ampersand in front of $value in the second foreach.  That will assign it by reference allowing you to change it on the fly.

assign $value as a reference in your foreach and it should work

. . .

Notice how I put an ampersand in front of $value in the second foreach.  That will assign it by reference allowing you to change it on the fly.

 

For some reason I could still not get it work, original values in the array remained unchanged. However, inspired by your first suggestion I was able to decode the offending characters already when creating the array from database search result. Looked like this:

 

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$rows[0] = html_entity_decode($row[0], ENT_COMPAT, 'UTF-8');   // just an example, actually looped through rows
$result_array[] = $row;
}

 

(The next step was to figure out how to sort the multidimensional array. Just in case someone googles this up, below is also an example of how to do it -- it may well be piece of cake to the brainiacs among us, but it took me quite a while to figure out. :) )

 

foreach ($result_array as $key => $row) {
$sort_by[$key]  = $row[0]; // replace [0] with the index to be used for sorting
}

array_multisort($sort_by, SORT_ASC, $result_array);
}

 

Thank you for your help Zanus, much appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.