xhaerlo - 11 months ago 86

PHP Question

I have an array that looks like this:

`a 12 34`

b 12345

c 123456

So the array looks like

`$array[0] = "a 12 34"`

$array[1] = "b 12345"

$array[2] = "c 123456"

I am trying to create an associative array such that

`[a] => 12 34`

[b] => 12345

[c] => 123456

Can I possibly split the array into two, one for containing "a, b, c" and another for their contents and use the

Answer Source

You can do that within a loop like the snippet below demonstrates. Quick-Test here:

```
$array = array("a 12 34", "b 12345", "c 123456");
$array2 = array();
foreach($array as $data){
preg_match("#([a-z])(\s)(.*)#i", $data, $matches);
list(, $key, $space, $value) = $matches;
$array2[$key] = $value;
}
var_dump($array2);
// YIELDS::
array(3) {
["a"]=> string(5) "12 34"
["b"]=> string(5) "12345"
["c"]=> string(6) "123456"
}
```