59

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.
  • 593
  • 1
  • 5
  • 5

10 Answers10

101

You will want to use str_split().

$result = str_split('abcdef');

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

dusk
  • 1,213
  • 1
  • 9
  • 9
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,023
  • 5
  • 31
  • 50
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,089
  • 4
  • 26
  • 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
  • 81,193
  • 17
  • 276
  • 242
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,295
  • 2
  • 17
  • 31
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
  • 782
  • 5
  • 26
2

You can use the str_split() function

$array = str_split($string);

foreach ($array as $p){

    echo $p . "<br />";
}
Pang
  • 9,073
  • 146
  • 84
  • 117
Nazca
  • 112
  • 1
  • 7
2

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
  • 1,960
  • 2
  • 25
  • 31
2

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) which respects newline characters by default. \X is slightly different from . without the s modifier. Demo

var_export(preg_split('/.\K/u', $string, 0, PREG_SPLIT_NO_EMPTY)); // element [1] has two characters in it!
echo "\n---\n";
var_export(preg_split('/.\K/us', $string, 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('/\X\K/u', $string, 0, PREG_SPLIT_NO_EMPTY));
mickmackusa
  • 37,596
  • 11
  • 75
  • 105
0

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,168
  • 2
  • 25
  • 44