Current Location: Home> Latest Articles> How to Use ord() and array_map() Together to Retrieve the ASCII Value of Each Character in a String?

How to Use ord() and array_map() Together to Retrieve the ASCII Value of Each Character in a String?

gitbox 2025-08-25

In PHP, the ord() function returns the ASCII value of a character, while the array_map() function can be used to apply a callback function to each element in an array. When we need to process every character in a string and retrieve their ASCII values in bulk, these two functions can be combined. This article will explain in detail how to implement this functionality.

1. Introduction to ord() Function

ord() is a built-in PHP function that returns the ASCII value of a character. For example:

<span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">ord</span></span><span>(</span><span><span class="hljs-string">&#039;A&#039;</span></span><span>);  </span><span><span class="hljs-comment">// Outputs 65</span></span><span>  
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">ord</span></span><span>(</span><span><span class="hljs-string">&#039;a&#039;</span></span><span>);  </span><span><span class="hljs-comment">// Outputs 97</span></span><span>  
</span></span>

This function accepts a single character as an argument and returns its ASCII value. For uppercase letters, numbers, lowercase letters, and other common characters, ord() correctly returns the corresponding ASCII value.

2. Introduction to array_map() Function

array_map() is an array function in PHP that allows you to apply a callback function to each element of an array and return a new array with the processed values. For example:

<span><span><span class="hljs-variable">$array</span></span><span> = [</span><span><span class="hljs-number">1</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>, </span><span><span class="hljs-number">3</span></span><span>];  
</span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_map</span></span><span>(function(</span><span><span class="hljs-variable">$item</span></span><span>) {  
    </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-variable">$item</span></span><span> * </span><span><span class="hljs-number">2</span></span><span>;  
}, </span><span><span class="hljs-variable">$array</span></span><span>);  
<p></span>print_r($result);  // Outputs [2, 4, 6]<br>
</span>

In the example above, array_map() applies a callback function to each element of the $array, multiplying each element by 2. The returned result is a new array with the processed values.

3. Using ord() and array_map() Together to Retrieve ASCII Values in Bulk

Now, we can combine these two functions to get the ASCII values of all characters in a string. First, we convert the string into an array of characters, then use array_map() to apply the ord() function to each character.

Code Implementation:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
<p></span>// Original string<br>
$str = "Hello World";</p>
<p>// Convert string into character array<br>
$charArray = str_split($str);</p>
<p>// Use array_map to get ASCII values in bulk<br>
$asciiValues = array_map('ord', $charArray);</p>
<p>// Output result<br>
print_r($asciiValues);</p>
<p>?><br>
</span>

Explanation:

  1. str_split($str): First, the string $str is split into a character array. For example, "Hello World" becomes ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'].

  2. array_map('ord', $charArray): Next, array_map() applies the ord() function to each character, returning the ASCII value of each one. For instance, the character 'H' is converted to 72, the character 'e' to 101, and so on.

  3. print_r($asciiValues): Finally, we print the array of ASCII values. The result is an array of integers representing the ASCII values of every character in the string.

Output Result:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>  
(  
    [</span><span><span class="hljs-number">0</span></span><span>] =&gt; </span><span><span class="hljs-number">72</span></span><span>  
    [</span><span><span class="hljs-number">1</span></span><span>] =&gt; </span><span><span class="hljs-number">101</span></span><span>  
    [</span><span><span class="hljs-number">2</span></span><span>] =&gt; </span><span><span class="hljs-number">108</span></span><span>  
    [</span><span><span class="hljs-number">3</span></span><span>] =&gt; </span><span><span class="hljs-number">108</span></span><span>  
    [</span><span><span class="hljs-number">4</span></span><span>] =&gt; </span><span><span class="hljs-number">111</span></span><span>  
    [</span><span><span class="hljs-number">5</span></span><span>] =&gt; </span><span><span class="hljs-number">32</span></span><span>  
    [</span><span><span class="hljs-number">6</span></span><span>] =&gt; </span><span><span class="hljs-number">87</span></span><span>  
    [</span><span><span class="hljs-number">7</span></span><span>] =&gt; </span><span><span class="hljs-number">111</span></span><span>  
    [</span><span><span class="hljs-number">8</span></span><span>] =&gt; </span><span><span class="hljs-number">114</span></span><span>  
    [</span><span><span class="hljs-number">9</span></span><span>] =&gt; </span><span><span class="hljs-number">108</span></span><span>  
    [</span><span><span class="hljs-number">10</span></span><span>] =&gt; </span><span><span class="hljs-number">100</span></span><span>  
)  
</span></span>

The output shows that the ASCII value of each character has been successfully extracted.

4. Summary

By combining the ord() and array_map() functions, we can easily retrieve the ASCII value of each character in a string in bulk. Using str_split() to split a string into a character array, and then applying ord() to each character with array_map(), results in a complete array of ASCII values. This method is both concise and efficient, making it ideal for scenarios that require batch processing of characters.