I have one string contains numbers and i need to convert it in an array. My string is:
["50000001","50000022","50000043","50000106"]
And i need to convert it in an array in PHP.
That string pretty much looks like JSON, so you could use json_decode()
:
$input = '["50000001","50000022","50000043","50000106"]';
$arr = json_decode( $input );
Try this -
$str = '["50000001","50000022","50000043","50000106"]';
$arr = json_decode($str, true);
var_dump($arr);
Looks like JSON, so you can use json_decode(), or you can look up to the explode function. A little more search effort would have brought you there.
http://php.net/manual/en/function.explode.php
None the less,
$string = " ["50000001","50000022","50000043","50000106"]";
explode(",", $string);
//or
json_decode($string);
Try this:
$string = '["50000001","50000022","50000043","50000106"]';
var_dump(explode(",", trim($string, "[]")));
If you do not need the quotes, you can also use this:
$string = '["50000001","50000022","50000043","50000106"]';
$array = str_replace('"', '', explode(",", trim($string, "[]")));
var_dump($array);
You can use json_decode:
$array = json_decode('["50000001","50000022","50000043","50000106"]');
Simple and effective.
The string looks like valid JSON, so use json_decode
for this:
$array = json_decode('["50000001","50000022","50000043","50000106"]');
var_dump($array);
// array(4) {
// [0]=>
// string(8) "50000001"
// [1]=>
// string(8) "50000022"
// [2]=>
// string(8) "50000043"
// [3]=>
// string(8) "50000106"
// }
If you also need to convert the integers-as-strings to integers, use intval
and array_map
:
$array = array_map("intval", $array);
var_dump($array);
// array(4) {
// [0]=>
// int(50000001)
// [1]=>
// int(50000022)
// [2]=>
// int(50000043)
// [3]=>
// int(50000106)
// }