Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a String into an Array of Characters

Tags:

arrays

string

php

People also ask

How do I convert a string to an array of arrays?

Split String into Array with split() The split() method is used to divide a string into an ordered list of two or more substrings, depending on the pattern/divider/delimiter provided, and returns it.

Can we convert a string to character array in Java?

Java For Testers Now, use the toCharArray() method to convert string to char array. char[] ch = str. toCharArray();


You will want to use str_split().

$result = str_split('abcdef');

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


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.


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.


$result = "abcdef";
$result = str_split($result);

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


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
)

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

Like below::

<?php 
$result = str_split('abcdef',1);
echo "<pre>";
print_r($result);
?>