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

How to Use ord() and array_map() Together to Retrieve ASCII Values 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 every element in an array. When we need to process each character of a string in bulk and obtain their ASCII values, these two functions can be combined. This article will explain in detail how to achieve this functionality.

1. Introduction to the 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 takes a single character as an argument and returns its ASCII value. For common characters such as uppercase letters, numbers, and lowercase letters, ord() correctly returns their ASCII values.

2. Introduction to the array_map() Function

array_map() is a PHP array function that allows you to apply a callback function to each element of an array, returning a new processed array. 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 above example, array_map() applies the callback function to each element of $array, multiplying each element by 2. The result is a new processed array.

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

Now, we can combine these two functions to retrieve the ASCII values of every character in a string. First, we convert the string into an array, 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 to 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 the result<br>
print_r($asciiValues);</p>
<p>?><br>
</span>

Explanation:

  1. str_split($str): First, split the string $str into an array of characters. 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. For example, the character 'H' becomes 72, the character 'e' becomes 101, and so on.

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

Output:

<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 result shows that the ASCII value of each character has been successfully extracted.

4. Conclusion

By combining ord() and array_map(), we can easily retrieve the ASCII values of every character in a string. Using str_split() to split the string into a character array and then applying ord() to each character via array_map(), we can generate a complete array of ASCII values. This approach is both simple and efficient, making it suitable for scenarios where bulk character processing is required.