feonil
feonil

Reputation: 9

PHP create associative array from list($key, $value)

I have an array that has resulted from an explode() function. The final operation I need to do is to separate each element based on an internal delimeter and load it all into an associative array. Something sort of like the following, which gets me the output ... but I can't figure out how to get my results into an array. Help please?

$string = 
item1:val1\n
item2:val2\n
item3:val3\n
item4:val4\n

$exploded = explode("\n",$string);
foreach($exploded as $iteration) {
list($key, $value) = explode(":",$iteration);
}

Upvotes: 0

Views: 1412

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 79004

Just build the array with the $key and $value:

$exploded = explode("\n",$string);
foreach($exploded as $iteration) {
    list($key, $value) = explode(":",$iteration);
    $result[$key] = $value;
}

print_r($result);

Upvotes: 1

Related Questions