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
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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.