Jump to content

String to array problem


rbrown

Recommended Posts

I have:

lscr|30,lsct|d,lsmcr|30,lsmct|d,msmcr|30,msmct|d,keancr|30,keanct|d,kswl|yes

 

And I want it to be returned as an array named returned with the first value as the key and the second value as the value:

 

$returned['lscr'] would equal 30

$returned['lsct'] would equal d

etc...

 

I'm just being stupid today and can't figure it out.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/92563-string-to-array-problem/
Share on other sites

this should work...

 

<?php
$orig_str = 'lscr|30,lsct|d,lsmcr|30,lsmct|d,msmcr|30,msmct|d,keancr|30,keanct|d,kswl|yes';
$keyval_array = explode(',', $orig_str);
foreach($keyval_array as $keyval){
$ele_arr = explode('|', $keyval);
$returned[$ele_arr[0]] = $ele_arr[1];
}
print_r($returned);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.