Get first character of array php

❮ PHP String Reference

Example

Return "world" from the string:

<?php
echo substr("Hello world",6);
?>

Try it Yourself »


Definition and Usage

The substr() function returns a part of a string.


Syntax

substr(string,start,length)

Parameter Values

ParameterDescription
string Required. Specifies the string to return a part of
start Required. Specifies where to start in the string
  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in string
length Optional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter
  • Negative number - The length to be returned from the end of the string
  • If the length parameter is 0, NULL, or FALSE - it return an empty string


Technical Details

Return Value:Returns the extracted part of a string, or FALSE on failure, or an empty string
PHP Version:4+
Changelog:PHP 7.0 - If string = start (in characters long), it will return an empty string. Earlier versions returns FALSE.
PHP 5.2.2 - 5.2.6 - If start has the position of a negative truncation, FALSE is returned. Other versions get the string from start.

More Examples

Example

Using the start parameter with different positive and negative numbers:

<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";

echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>

Try it Yourself »

Example

Using the start and length parameters with different positive and negative numbers:

<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";

echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>

Try it Yourself »


❮ PHP String Reference


In this tutorial, we will represent to you three main PHP functions used for removing the first character of a string.

Check out the examples and choose the one that suits more for your project.

The first PHP function that you can use for removing the first character of a string is Itrim().

All you need to do is just to apply the code below:

$str = '::f:o:'; 
$str = ltrim($str, ':'); 
var_dump($str); //=> 'f:o:'

The second way we recommend you to use is the substr() function. Here is how to run it:

And the last handy function to use for removing the last character of a string is preg_replace():

$str = "hello"; 
$str = preg_replace('/^./', '', $str);

The Itrim() function is supported on PHP 4, PHP 5, and PHP 7 versions. This function is used for returning a string with whitespace stripped from the beginning of the string.

The substr() is considered a built-in function in PHP, applied for extracting a part of a string.

It is capable of returning the extracted part of a string if successful. On failure, it will return either false or an empty string.

The preg_replace() function is capable of returning a string or an array of strings. Here, all the matches of a pattern, detected in the output are replaced with substrings.

I've got an array that looks like this:

[workorders_hours] => Array
    (
        [213] => Array
            (
                [0] => Array
                    (
                        [hours] => 5.00
                        [rate] => 20.00
                    )

                [1] => Array
                    (
                        [hours] => 10.00
                        [rate] => 30.00
                    )

            )

        [215] => Array
            (
                [0] => Array
                    (
                        [hours] => 12.00
                        [rate] => 24.00
                    )

            )

    )

So when I loop through my array like so:

foreach ($results['workorders_hours'] as $workorders_hours) {
    foreach($workorders_hours as $workorder) {
        echo "<pre>";
        print_r($workorder);
        echo "</pre>";
    }
}

I get

Array
  (
    [hours] => 5.00
    [rate] => 20.00
  )

Array
  (
    [hours] => 10.00
    [rate] => 30.00
  )

Array
  (
    [hours] => 12.00
    [rate] => 24.00
  )

So you would think I could do this

foreach ($results['workorders_hours'] as $workorders_hours) {
    foreach($workorders_hours as $workorder) {
        foreach($workorder as $time) {
            echo $time['hours'];
            echo $time['rate'];
        }
    }
}

And get the values for the hour and rate, but instead, I get

552211331122

So it's only giving me the first character for each item in the $time array. What's the deal?

How do I get the first character of a string array in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.

How do I get the first character of a word in PHP?

get each word as a row in an array..
reduce that array to the first letter. $acronym = array_reduce( str_word_count("Community College District", 1), function($res , $w){ return $res . $ w[0]; } );.

How do I print the first letter of a string in PHP?

mb_substr. It takes into consideration text encoding. Example: $first_character = mb_substr($str, 0, 1).
substr. Example: $first_character = substr($str, 0, 1);.
Using brackets ( [] ) Avoid as it throws a notice in PHP 7.x and a warning in PHP 8.x if the string is empty. Example: $first_character = $str[0];.

How do I get the first index of a string in PHP?

The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive.