Jump to content

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


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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.