This should work for you:
First I split the string with preg_split()
and use a new line as delimiter (
), where I also consume all spaces (\s*
) on the right and left side, so you don't have them in the key:
preg_split("/\s*
\s*/", $string)
Then I go through each pair of numbers (x:y
) with array_map()
and explode()
it by a colon. So you end up with an array like this:
Array
(
[0] => Array
(
[0] => 763
[1] => 74
)
[1] => Array
(
[0] => 74
[1] => 274
)
[2] => Array
(
[0] => 177
[1] => 474
)
)
At the end I use array_column()
to say, that you want to use the 0st column as key and the 1st column as value.
Code:
<?php
$string = '763:74
74:274
177:474';
$result = array_column(
array_map(function($v){
return explode(":", $v);
}, preg_split("/\s*
\s*/", $string)
), 1, 0);
print_r($result);
?>
output:
Array
(
[763] => 74
[74] => 274
[177] => 474
)