switz
switz

Reputation: 25188

Convert basic array to PHP array

I have an array that I pull that looks like this:

[157966745,275000353,43192565,305328212]...

How do I go about taking that "string" and converting it to a PHP array which I can then manipulate.

Upvotes: 1

Views: 107

Answers (5)

Trey
Trey

Reputation: 5520

The answer by @Felix Kling is much more appropriate given the context of the question.

$new_string = preg_replace('/([\[|\]])/', '', $strarr);
$new_array = explode(',', $new_string);`

Upvotes: 0

RRStoyanov
RRStoyanov

Reputation: 1172

With exact that code...

$string='[157966745,275000353,43192565,305328212]';
$newString=str_replace(array('[', ']'), '', $string); // remove the brackets
$createArray=explode(',', $newString); // explode the commas to create an array

print_r($createArray);

Upvotes: 2

Tomalak
Tomalak

Reputation: 338228

$s = "[157966745,275000353,43192565,305328212]";

$matches;
preg_match_all("/\d+/", $s, $matches);

print_r($matches);

Upvotes: 2

Felix Kling
Felix Kling

Reputation: 816462

This looks like JSON, so you can use json_decode:

$str = "[157966745,275000353,43192565,305328212]";
$data = json_decode($str);

Upvotes: 10

Dave Kiss
Dave Kiss

Reputation: 10485

PHP explode is built just for this.

$result = explode(',', $input)

Upvotes: 0

Related Questions