My variable like this,
$a = "A1|A2|A3|";
so how to convert php array?
to this,
$a=array("A1","A2","A3");
First, trim off the pipe at the end of your input.
$str = rtrim($a, '|');
Then, explode on the pipe.
$a = explode('|', $str);
Alternatively, do the split normally and then filter out the whitespace entries as a post-processing step.
$a = array_filter($a, 'strlen');
| or else you will get last empty element in array.You can do achieve what you want to this way:
$a = "A1|A2|A3|";
$a = rtrim($a, '|'); // Trimming the final |
$exploded = explode("|", $a); // Extracting the values from string.
var_dump($exploded);
Hope it answers your question. Refer rtrim and explode on official documentation site for more relevant details.
$a = "A1|A2|A3|";
$newA = explode("|",$a);
print_r($newA);
$a=explode('|',$a);
Check this link
explode — Split a string by string
Description: array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
$a = explode('|', $a);It's one of the functions most people learn before anything else