0

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);
}
2
  • You take $key and $value and put them into an array. Commented Nov 24, 2014 at 19:55
  • 2
    How about declaring array variable before loop, then assigning $arr[$key] = $value; inside loop? Commented Nov 24, 2014 at 19:55

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't answer such questions, they deserve only simple hint and a close vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.