60

In PHP, how do I convert:

$result = "abdcef";

into an array that's:

$result[0] = a;
$result[1] = b;
$result[2] = c;
$result[3] = d;

Edited

AJ.
  • 603
  • 1
  • 6
  • 5

11 Answers11

104

You will want to use str_split().

$result = str_split('abcdef');

http://us2.php.net/manual/en/function.str-split.php

dusk
  • 1,243
  • 1
  • 9
  • 10
30

Don't know if you're aware of this already, but you may not need to do anything (depending on what you're trying to do).

$string = "abcdef";
echo $string[1];
//Outputs "b"

So you can access it like an array without any faffing if you just need something simple.

BT643
  • 3,495
  • 5
  • 34
  • 55
5

You can use the str_split() function:

$value = "abcdef";
$array = str_split($value);

If you wish to divide the string into array values of different amounts you can specify the second parameter:

$array = str_split($value, 2);

The above will split your string into an array in chunks of two.

Chaim
  • 2,109
  • 4
  • 27
  • 48
4
$result = "abcdef";
$result = str_split($result);

There is also an optional parameter on the str_split function to split into chunks of x characters.

Gazler
  • 83,029
  • 18
  • 279
  • 245
4

best you should go for "str_split()", if there is need to manual Or basic programming,

    $string = "abcdef";
    $resultArr = [];
    $strLength = strlen($string);
    for ($i = 0; $i < $strLength; $i++) {
        $resultArr[$i] = $string[$i];
    }
    print_r($resultArr);

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
Gautam Rai
  • 2,445
  • 2
  • 21
  • 31
4

https://www.php.net/manual/en/function.str-split.php#refsect1-function.str-split-notes

str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string.

Use mb_str_split() instead.

GTCrais
  • 2,039
  • 2
  • 26
  • 32
4

If you need multibyte support in an outdated version of PHP (a version below PHP7.4), then use preg_split() on an empty pattern with a unicode flag. There is no need to slow down the regex engine with a capture group.

Code: (Demo)

var_export(
    preg_split('//u', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
    // or preg_split('/.\K/us', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
    // or preg_split('/\X\K/u', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
);

For any versions of PHP from 7.4 or higher, just use the dedicated function mb_str_split().

mb_str_split('abcåäö')

Output:

array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
  3 => 'å',
  4 => 'ä',
  5 => 'ö',
) 

As a warning to researchers using other answers on this page, if you use square brace syntax to access characters by their offset or you use str_split(), you will get broken results when dealing with multibyte characters.

For anyone doing thorough research, I should also mention the \X (unicode version of the dot) respects newline characters by default. \X is slightly different from . without the s modifier. Demo

var_export(preg_split('/.\K/u', "å\nä", 0, PREG_SPLIT_NO_EMPTY)); // element [1] has two characters in it!
echo "\n---\n";
var_export(preg_split('/.\K/us', "å\nä", 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('/\X\K/u', "å\nä", 0, PREG_SPLIT_NO_EMPTY));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
3

With the help of str_split function, you will do it.

Like below::

<?php 
$result = str_split('abcdef',1);
echo "<pre>";
print_r($result);
?>
Pratik
  • 810
  • 6
  • 26
2

You can use the str_split() function

$array = str_split($string);

foreach ($array as $p){

    echo $p . "<br />";
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Nazca
  • 112
  • 1
  • 7
1

str_split() is not safe for multibyte characters.

mb_str_split() requires PHP 7.4+.

Try preg_split() for the rescuse:

$result = preg_split('/(.)/u', 'abcåäö', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
tim
  • 2,530
  • 3
  • 26
  • 45
0

If you tried the above ways and did not get results, try the following method. It worked for my language (Persian and Arabic):

$result = [];
for ( $i = 0; $i < mb_strlen( $string ); ++ $i ) {
    $result[] = mb_substr( $string, $i, 1, 'UTF-8' );
}
var_dump($result);
  • Of course, it also works correctly for English. The point of this method is to use "mb_substr" along with "UTF-8" instead of "substr". – Ali Emadzadeh Jul 22 '22 at 14:43